/// <summary>
        /// Called when a .NET event that must be propagated to Flutter is raised.
        /// This method propagates the event through <see cref="OnPlatformEvent"/>
        /// so that <see cref="FlutnetBridge"/> can subscribe and send data to Flutter.
        /// </summary>
        private static void PropagatePlatformEvent(string serviceName, string eventName, object sender, EventArgs eventArgs)
        {
            OnPlatformEventArgs args = new OnPlatformEventArgs
            {
                ServiceName = serviceName,
                EventName   = eventName,
                EventData   = eventArgs
            };

            OnPlatformEvent?.Invoke(sender, args);
        }
예제 #2
0
        private void FlutnetRuntimeOnPlatformEvent(object sender, OnPlatformEventArgs e)
        {
            // Prevent dispatching events to Flutter through event channel
            // if bridge is configured for WebSocket communication
            if (Mode == FlutnetBridgeMode.WebSocket)
            {
                return;
            }

            FlutnetEventInfo eventInfo = new FlutnetEventInfo
            {
                InstanceId = e.ServiceName,
                EventName  = e.EventName.FirstCharLower(),
                EventData  = e.EventData
            };

            NSObject eventValue = FlutterInterop.ToMethodChannelResult(eventInfo);

            // As stated here https://docs.microsoft.com/en-US/xamarin/essentials/main-thread#determining-if-code-is-running-on-the-main-thread:
            // The platform implementations of BeginInvokeOnMainThread themselves check if the call is made on the main thread.
            // There is very little performance penalty if you call BeginInvokeOnMainThread when it's not really necessary.

            if (!MainThread.IsMainThread)
            {
                MainThread.BeginInvokeOnMainThread(() =>
                {
                    try
                    {
                        _streamHandler.EventSink?.Invoke(eventValue);
                    }
                    catch (Exception ex)
                    {
                        // TODO: Properly log any error
                        Console.WriteLine(ex.Message);
                    }
                });
            }
            else
            {
                try
                {
                    _streamHandler.EventSink?.Invoke(eventValue);
                }
                catch (Exception ex)
                {
                    // TODO: Properly log any error
                    Console.WriteLine(ex.Message);
                }
            }
        }
        private void FlutnetRuntimeOnPlatformEvent(object sender, OnPlatformEventArgs e)
        {
            // Prevent dispatching events to Flutter through event channel
            // if bridge is configured for WebSocket communication
            if (Mode == FlutnetBridgeMode.WebSocket)
            {
                return;
            }

            FlutnetEventInfo eventInfo = new FlutnetEventInfo
            {
                InstanceId = e.ServiceName,
                EventName  = e.EventName.FirstCharLower(),
                EventData  = e.EventData
            };

            Object eventValue = FlutterInterop.ToMethodChannelResult(eventInfo);

            if (!MainThread.IsMainThread)
            {
                MainThread.BeginInvokeOnMainThread(() =>
                {
                    try
                    {
                        _streamHandler.EventSink?.Success(eventValue);
                    }
                    catch (Exception ex)
                    {
                        // TODO: Properly log any error
                        Console.WriteLine(ex.Message);
                    }
                });
            }
            else
            {
                try
                {
                    _streamHandler.EventSink?.Success(eventValue);
                }
                catch (Exception ex)
                {
                    // TODO: Properly log any error
                    Console.WriteLine(ex.Message);
                }
            }
        }
        private void FlutnetRuntimeOnPlatformEvent(object sender, OnPlatformEventArgs e)
        {
            FlutnetEventInfo eventInfo = new FlutnetEventInfo
            {
                InstanceId = e.ServiceName,
                EventName  = e.EventName.FirstCharLower(),
                EventData  = e.EventData
            };

            FlutnetMessage message = new FlutnetMessage()
            {
                MethodInfo = new FlutnetMethodInfo
                {
                    RequestId = -1,
                    Instance  = e.ServiceName
                },
                Result    = null,
                EventInfo = eventInfo
            };

            Send(message);
        }