/// <summary> /// Initializes a new instance of the <see cref="FlutnetBridge"/> class /// specifying how platform code and Flutter code communicate. /// </summary> public FlutnetBridge(FlutterEngine engine, FlutnetBridgeMode mode) { // Create the named channel for communicating with Flutter module using asynchronous method calls // NOTE: This channel is used to RECEIVE messages/requests FROM Flutter _methodChannelIncoming = FlutterMethodChannel.Create("flutnetbridge.incoming", engine.BinaryMessenger); _methodChannelIncoming.SetMethodCallHandler(HandleMethodCall); // Create a second named channel for diagnostic use only. // This channel is used, for example, to test if Flutter module is running // embedded into a native Xamarin app or as a standalone app _methodChannelTest = FlutterMethodChannel.Create("flutnetbridge.support", engine.BinaryMessenger); _methodChannelTest.SetMethodCallHandler(HandleMethodCallTest); // Create the named channel for communicating with Flutter module using event streams // NOTE: This channel is used to SEND messages/notifications TO Flutter // An event channel is a specialized platform channel intended for the use case of exposing platform events to Flutter as a Dart stream. // The Flutter SDK currently has no support for the symmetrical case of exposing Dart streams to platform code, though that could be built, if the need arises. // see: https://medium.com/flutter/flutter-platform-channels-ce7f540a104e _streamHandler = new StreamHandler(this); _eventChannel = FlutterEventChannel.Create("flutnetbridge.outgoing", engine.BinaryMessenger); _eventChannel.SetStreamHandler(_streamHandler); Mode = mode; FlutnetRuntime.OnPlatformEvent += FlutnetRuntimeOnPlatformEvent; if (Mode == FlutnetBridgeMode.WebSocket) { _socketService = new FlutnetWebSocketService(); } }
void ButtonClicked() { var vc = new FlutterViewController(AppDelegate.FlutterEngine, null, null); PresentViewController(vc, true, null); var channel = FlutterMethodChannel.MethodChannelWithName("diversido.io/main", vc.BinaryMessenger); channel.InvokeMethod("reset", NSNumber.FromNInt(11)); channel.SetMethodCallHandler((FlutterMethodCall call, FlutterResult result) => { if (call.Method == "increment") { var counter = (int)(NSNumber)call.Arguments; try { result((NSNumber)(counter + 1)); } catch (Exception e) { Debug.WriteLine(e); } } }); }
void init() { if (isInitalized) { return; } FlutterHotReloadHelper.HotReloadHandler = this; isInitalized = true; GeneratedPluginRegistrant.Register(Engine); MethodChannel = FlutterMethodChannel.FromNameAndMessenger("com.Microsoft.FlutterSharp/Messages", Engine.BinaryMessenger); Flutter.Internal.Communicator.SendCommand = (x) => MethodChannel.InvokeMethod(x.Method, (NSString)x.Arguments); MethodChannel.SetMethodCaller((call, result) => { if (call.Method == "ready" && Widget != null) { isReady = true; FlutterManager.SendState(Widget); } Flutter.Internal.Communicator.OnCommandReceived?.Invoke((call.Method, call.Arguments.ToString(), (x) => { result((NSString)x); } )); }); }