gpt4 book ai didi

xamarin - 将数据从 Android 传递到 PCL 时未收到 MessagingCenter 消息

转载 作者:行者123 更新时间:2023-12-05 02:18:18 26 4
gpt4 key购买 nike

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

在主事件中:

if(data != null)
{

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

}

在 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");
});

感谢您的帮助。

最佳答案

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

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

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

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

取消订阅

MessagingCenter.Unsubscribe<MainPage, string> (this, "MapIntentReceived");

关于xamarin - 将数据从 Android 传递到 PCL 时未收到 MessagingCenter 消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45740892/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com