Will Nasby
Will Nasby

Reputation: 1148

MessagingCenter message not being received when data is passed from Android to PCL

I'm trying to send back data from MainActivity to a PCL view. When I send a message without data, it receives it. But when I try to pass a string back with it, the code is never reached.

In MainActivity:

if(data != null)
            {

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

            }

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

Thanks for the help.

Upvotes: 3

Views: 1478

Answers (1)

Pavan V Parekh
Pavan V Parekh

Reputation: 1946

To send the message with argument, include the Type generic parameter and the value of the argument in the Send method call.

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

To pass an argument with the message, specify the argument Type in the Subscribe generic arguments and in the Action signature.

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

For unsubscribe

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

Upvotes: 5

Related Questions