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

时间:2017-08-17 16:49:18

标签: xamarin xamarin.forms

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

在MainActivity中:

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

感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

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

x10._CON

要使用消息传递参数,请在Subscribe generic arguments和Action signature中指定参数Type。

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

取消订阅

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