Exemplo n.º 1
0
        //---------------------------------------------------------------------------------------
        // This code follows the architecture drawn by DotNetBrowser
        // (cf https://dotnetbrowser.support.teamdev.com/support/solutions/articles/9000109868-calling-javascript-from-net)
        // For an example of implementation, go to INTERNAL_InteropImplementation.cs and
        // ExecuteJavaScript_SimulatorImplementation method, in the first "if".
        //---------------------------------------------------------------------------------------

#if OPENSILVER
        private static object OnCallbackFromJavaScript <T>(
            int callbackId,
            string idWhereCallbackArgsAreStored,
            T callbackArgsObject,
            Func <int, int, string, T, Type[], object[]> makeArguments,
            bool isInSimulator,
            bool returnValue)
        {
            object result         = null;
            var    actionExecuted = false;
            Action action         = () =>
            {
                INTERNAL_SimulatorExecuteJavaScript.RunActionThenExecutePendingAsyncJSCodeExecutedDuringThatAction(
                    () =>
                {
                    //--------------------
                    // Call the callback:
                    //--------------------
                    try
                    {
                        result         = CallMethod(callbackId, idWhereCallbackArgsAreStored, makeArguments, callbackArgsObject);
                        actionExecuted = true;
                    }
                    catch (Exception ex)
                    {
#if DEBUG
                        Console.Error.WriteLine("DEBUG: OnCallBack: OnCallBackFromJavascript: " + ex);
#endif
                        throw;
                    }
                });
            };

            if (isInSimulator)
            {
                // Go back to the UI thread because DotNetBrowser calls the callback from the socket background thread:
                if (returnValue)
                {
                    var timeout = TimeSpan.FromSeconds(30);
                    INTERNAL_Simulator.WebControlDispatcherInvoke(action, timeout);
                    if (!actionExecuted)
                    {
                        GenerateDeadlockException(timeout);
                    }
                }
                else
                {
                    INTERNAL_Simulator.WebControlDispatcherBeginInvoke(action);
                }
            }
            else
            {
                action();
            }

            return(returnValue ? result : null);
        }
Exemplo n.º 2
0
        //---------------------------------------------------------------------------------------
        // This code follows the architecture drawn by DotNetBrowser (cf https://dotnetbrowser.support.teamdev.com/support/solutions/articles/9000109868-calling-javascript-from-net)
        // For an example of implementation, go to INTERNAL_InteropImplementation.cs & ExecuteJavaScript_SimulatorImplementation method, in the first "if".
        //---------------------------------------------------------------------------------------


#if CSHTML5NETSTANDARD
        // In The OpenSilver Version, there are 2 options for callback:
        // - Browser   Version: the callback is called from js but using the Microsoft Interop. It uses an object[]
        // - Simulator Version: the callback is called from js but using the DotNetBrowser Interop. It uses a  JSArray

        static public void OnCallbackFromJavaScript(
            string callbackIdString,
            string idWhereCallbackArgsAreStored,
            object[] callbackArgsString
            )
        {
            //Console.WriteLine("ID string: " + callbackIdString);
            int callbackId = int.Parse(callbackIdString);

            object[] callbackArgsObject = new object[0];
            Action   action             = () =>
            {
                //----------------------------------
                // Get the C# callback from its ID:
                //----------------------------------
                Delegate callback = _dictionary[callbackId];

                Type   callbackType        = callback.GetType();
                Type[] callbackGenericArgs = null;
                if (callbackType.IsGenericType)
                {
                    callbackGenericArgs = callbackType.GetGenericArguments();
                    callbackType        = callbackType.GetGenericTypeDefinition();
                }
                var callbackArgs = callbackArgsObject;

                //--------------------
                // Call the callback:
                //--------------------
#if CATCH_INTEROP_EXCEPTIONS // Note: previously, when using the "Awesomium" browser control in the Simulator, we handled the exceptions because otherwise they got captured by the WebBrowser control, due to the fact that the callback is executed in the context of the WebBrowser control. This is no longer true with the "DotNetBrowser" control.
                try
                {
#endif
                try
                {
                    if (callbackType == typeof(Action))
                    {
                        DelegateDynamicInvoke(callback);
                    }
                    else if (callbackType == typeof(Action <>))
                    {
                        DelegateDynamicInvoke(callback, MakeArgumentsForCallback(1, callbackId, idWhereCallbackArgsAreStored, callbackArgs, callbackGenericArgs));
                    }
                    else if (callbackType == typeof(Action <,>))
                    {
                        DelegateDynamicInvoke(callback, MakeArgumentsForCallback(2, callbackId, idWhereCallbackArgsAreStored, callbackArgs, callbackGenericArgs));
                    }
                    else if (callbackType == typeof(Action <, ,>))
                    {
                        DelegateDynamicInvoke(callback, MakeArgumentsForCallback(3, callbackId, idWhereCallbackArgsAreStored, callbackArgs, callbackGenericArgs));
                    }
                    else if (callbackType == typeof(Action <, , ,>))
                    {
                        DelegateDynamicInvoke(callback, MakeArgumentsForCallback(4, callbackId, idWhereCallbackArgsAreStored, callbackArgs, callbackGenericArgs));
                    }
                    else if (callbackType == typeof(Action <, , , ,>))
                    {
                        DelegateDynamicInvoke(callback, MakeArgumentsForCallback(5, callbackId, idWhereCallbackArgsAreStored, callbackArgs, callbackGenericArgs));
                    }
                    else if (callbackType == typeof(Action <, , , , ,>))
                    {
                        DelegateDynamicInvoke(callback, MakeArgumentsForCallback(6, callbackId, idWhereCallbackArgsAreStored, callbackArgs, callbackGenericArgs));
                    }
                    else if (callbackType == typeof(Action <, , , , , ,>))
                    {
                        DelegateDynamicInvoke(callback, MakeArgumentsForCallback(7, callbackId, idWhereCallbackArgsAreStored, callbackArgs, callbackGenericArgs));
                    }
                    else if (callbackType == typeof(Action <, , , , , , ,>))
                    {
                        DelegateDynamicInvoke(callback, MakeArgumentsForCallback(8, callbackId, idWhereCallbackArgsAreStored, callbackArgs, callbackGenericArgs));
                    }
                    else if (callbackType == typeof(Action <, , , , , , , ,>))
                    {
                        DelegateDynamicInvoke(callback, MakeArgumentsForCallback(9, callbackId, idWhereCallbackArgsAreStored, callbackArgs, callbackGenericArgs));
                    }
                    else
                    {
                        throw new Exception(string.Format("Callback type not supported: {0}  Please report this issue to [email protected]", callbackType.ToString()));
                    }
                }
                catch (Exception ex)
                {
#if DEBUG
                    Console.Error.WriteLine("DEBUG: OnCallBack: OnCallBackFromJavascript: " + ex);
#endif
                    throw;
                }
#if CATCH_INTEROP_EXCEPTIONS     // Note: previously, when using the "Awesomium" browser control in the Simulator, we handled the exceptions because otherwise they got captured by the WebBrowser control, due to the fact that the callback is executed in the context of the WebBrowser control. This is no longer true with the "DotNetBrowser" control.
            }
            catch (Exception ex) // We catch and handle the Exception because otherwise it gets captured by the WebBrowser control, due to the fact that the callback is executed in the context of the WebBrowser control.
            {
                string message = ex.Message;

                if (Interop.IsRunningInTheSimulator && ex.InnerException != null)     // Note: "InnerException" is only supported in the Simulator as of July 27, 2017.
                {
                    message += Environment.NewLine + Environment.NewLine + ex.InnerException.Message
                               + Environment.NewLine + Environment.NewLine + ex.InnerException.StackTrace;
                }
                else
                {
                    message += Environment.NewLine + Environment.NewLine + ex.StackTrace;
                }


                if (Interop.IsRunningInTheSimulator)
                {
                    // Go back to the UI thread because DotNetBrowser calls the callback from the socket background thread:
                    INTERNAL_Simulator.WebControl.Dispatcher.BeginInvoke((Action)(() =>
                    {
                        MessageBox.Show(message);
                    }));
                }
                else
                {
                    MessageBox.Show(message);
                }
            }
#endif
            };

            if (Interop.IsRunningInTheSimulator)
            {
                // Run the action and then flush any pending async JavaScript code that was executed during that action:
                INTERNAL_SimulatorExecuteJavaScript.RunActionThenExecutePendingAsyncJSCodeExecutedDuringThatAction(
                    action
                    );
            }
            else
            {
                action();
            }
        }
Exemplo n.º 3
0
        public static void OnCallbackFromJavaScript(
            int callbackId,
            string idWhereCallbackArgsAreStored,
            object[] callbackArgsObject)
        {
            Action action = () =>
            {
                INTERNAL_SimulatorExecuteJavaScript.RunActionThenExecutePendingAsyncJSCodeExecutedDuringThatAction(
                    () =>
                {
                    //----------------------------------
                    // Get the C# callback from its ID:
                    //----------------------------------
                    Delegate callback = _dictionary[callbackId];

                    Type callbackType          = callback.GetType();
                    Type[] callbackGenericArgs = null;
                    if (callbackType.IsGenericType)
                    {
                        callbackGenericArgs = callbackType.GetGenericArguments();
                        callbackType        = callbackType.GetGenericTypeDefinition();
                    }
                    var callbackArgs = callbackArgsObject;

                    //--------------------
                    // Call the callback:
                    //--------------------
                    try
                    {
                        if (callbackType == typeof(Action))
                        {
                            DelegateDynamicInvoke(callback);
                        }
                        else if (callbackType == typeof(Action <>))
                        {
                            DelegateDynamicInvoke(callback, MakeArgumentsForCallback(1, callbackId, idWhereCallbackArgsAreStored, callbackArgs, callbackGenericArgs));
                        }
                        else if (callbackType == typeof(Action <,>))
                        {
                            DelegateDynamicInvoke(callback, MakeArgumentsForCallback(2, callbackId, idWhereCallbackArgsAreStored, callbackArgs, callbackGenericArgs));
                        }
                        else if (callbackType == typeof(Action <, ,>))
                        {
                            DelegateDynamicInvoke(callback, MakeArgumentsForCallback(3, callbackId, idWhereCallbackArgsAreStored, callbackArgs, callbackGenericArgs));
                        }
                        else if (callbackType == typeof(Action <, , ,>))
                        {
                            DelegateDynamicInvoke(callback, MakeArgumentsForCallback(4, callbackId, idWhereCallbackArgsAreStored, callbackArgs, callbackGenericArgs));
                        }
                        else if (callbackType == typeof(Action <, , , ,>))
                        {
                            DelegateDynamicInvoke(callback, MakeArgumentsForCallback(5, callbackId, idWhereCallbackArgsAreStored, callbackArgs, callbackGenericArgs));
                        }
                        else if (callbackType == typeof(Action <, , , , ,>))
                        {
                            DelegateDynamicInvoke(callback, MakeArgumentsForCallback(6, callbackId, idWhereCallbackArgsAreStored, callbackArgs, callbackGenericArgs));
                        }
                        else if (callbackType == typeof(Action <, , , , , ,>))
                        {
                            DelegateDynamicInvoke(callback, MakeArgumentsForCallback(7, callbackId, idWhereCallbackArgsAreStored, callbackArgs, callbackGenericArgs));
                        }
                        else if (callbackType == typeof(Action <, , , , , , ,>))
                        {
                            DelegateDynamicInvoke(callback, MakeArgumentsForCallback(8, callbackId, idWhereCallbackArgsAreStored, callbackArgs, callbackGenericArgs));
                        }
                        else if (callbackType == typeof(Action <, , , , , , , ,>))
                        {
                            DelegateDynamicInvoke(callback, MakeArgumentsForCallback(9, callbackId, idWhereCallbackArgsAreStored, callbackArgs, callbackGenericArgs));
                        }
                        else
                        {
                            throw new Exception(string.Format("Callback type not supported: {0}  Please report this issue to [email protected]", callbackType.ToString()));
                        }
                    }
                    catch (Exception ex)
                    {
#if DEBUG
                        Console.Error.WriteLine("DEBUG: OnCallBack: OnCallBackFromJavascript: " + ex);
#endif
                        throw;
                    }
                });
            };

            if (Interop.IsRunningInTheSimulator_WorkAround)
            {
                // Go back to the UI thread because DotNetBrowser calls the callback from the socket background thread:
                INTERNAL_Simulator.WebControlDispatcherBeginInvoke(action);
            }
            else
            {
                action();
            }
        }