Пример #1
0
        /// <summary>
        /// Registers a method call handler on this channel.
        /// </summary>
        /// <param name="handler">A <see cref="MethodCallHandler"/>, or null to deregister.</param>
        public void SetMethodCallHandler(MethodCallHandler handler)
        {
            async Task <byte[]> binaryHandler(byte[] bytes)
            {
                MethodCall call = Codec.DecodeMethodCall(bytes);

                try
                {
                    return(Codec.EncodeSuccessEnvelope(await handler(call)));
                }
                catch (FlutterException e)
                {
                    return(Codec.EncodeErrorEnvelope(e.Code, e.Message, e.Details, e.StackTrace));
                }
                catch (MissingPluginException)
                {
                    return(null);
                }
                catch (Exception e)
                {
                    return(Codec.EncodeErrorEnvelope("error", e.Message, null, e.StackTrace));
                }
            }

            BinaryMessenger.SetMessageHandler(Name, handler == null ? null : (BinaryMessageHandler)binaryHandler);
        }
Пример #2
0
 /// <summary>
 /// Registers a method call handler on this channel.
 /// </summary>
 /// <param name="handler">An asynchronous callback function to handle the <see cref="MethodCall">.</param>
 public void SetMethodCallHandler(Func <MethodCall, Task <object> > handler)
 {
     if (handler == null)
     {
         throw new ArgumentNullException(nameof(handler));
     }
     BinaryMessenger.SetMessageHandler(Name, async(byte[] bytes) =>
     {
         MethodCall call = Codec.DecodeMethodCall(bytes);
         try
         {
             return(Codec.EncodeSuccessEnvelope(await handler(call)));
         }
         catch (FlutterException e)
         {
             return(Codec.EncodeErrorEnvelope(e.Code, e.Message, e.Details, e.StackTrace));
         }
         catch (MissingPluginException)
         {
             return(null);
         }
         catch (Exception e)
         {
             return(Codec.EncodeErrorEnvelope("error", e.Message, null, e.StackTrace));
         }
     });
 }