当数据从 Android 传递到 PCL 时,未收到 MessagingCenter 消息

Wil*_*sby 3 xamarin xamarin.forms

我正在尝试将数据从 MainActivity 发送回 PCL 视图。当我发送一条没有数据的消息时,它会收到它。但是当我尝试用它传回一个字符串时,代码永远不会到达。

在主要活动中:

if(data != null)
            {

                MessagingCenter.Send<object, string>(this, data, "MapIntentReceived");
                MessagingCenter.Send<object>(this, "MapIntentReceived");

            }
Run Code Online (Sandbox Code Playgroud)

在 PCL 中:

            MessagingCenter.Subscribe<object, string>(this, "MapIntentReceived",
                async (sender, roomString) =>
                {   //his code is not reached
                    await SearchForRooms(roomString);
                });

            MessagingCenter.Subscribe<object>(this, "MapIntentReceived",
                async (sender) =>
                {   //this code is reached
                    await SearchForRooms("blah");
                });
Run Code Online (Sandbox Code Playgroud)

谢谢您的帮助。

Pav*_*ekh 5

要发送带有参数的消息,请在 Send 方法调用中包含 Type 通用参数和参数值。

MessagingCenter.Send<MainPage, string> (this, "MapIntentReceived", data);
Run Code Online (Sandbox Code Playgroud)

要随消息传递参数,请在订阅通用参数和操作签名中指定参数类型。

MessagingCenter.Subscribe<MainPage, string> (this, "MapIntentReceived", (sender, arg) => {
    await SearchForRooms(arg);
});
Run Code Online (Sandbox Code Playgroud)

对于取消订阅

MessagingCenter.Unsubscribe<MainPage, string> (this, "MapIntentReceived");
Run Code Online (Sandbox Code Playgroud)