I just lodged this as a bug:
https://github.com/xamarin/Xamarin.Forms/issues/5809
But was wondering if anyone else is seeing this.
Description
Generally a MessageCenter message subscription it for a message sent from a specific class.
But it can also be anonymous in the sense that any class can .Send
a message and any class will hear it.
For example:
Subscribe:
MessagingCenter.Subscribe<string>(this,"ISENGINEON",OnEngineOnChanged);//Subscribe without caring who is sending the message.
Send:
MessagingCenter.Send("ISENGINEON", true.ToString());//Shared layer view models subscribe and react
You see it discussed with successful results back in Jan of 2018
https://stackoverflow.com/questions/48047444/xamarin-android-how-do-i-pass-an-event-from-mainactivity-to-viewmodel-on-forms
Yet... Now, it seem to no longer work. I'm testing in VS2019 with both 3.6.x stable and 4.0-pre
The subscribe takes place... the send takes place... The subscriber however never hears the message and thus never executes the handler method.
Steps to Reproduce
- Any simple Xamarin app. "Welcome to Xamarin Forms" is fine. Add a button on the page
<Button
x:Name="Test"
Clicked="Test_OnClicked"
Text="Test" />
- Add a handler for the button in the code behind
private void Test_OnClicked(object sender, EventArgs e)
{
Console.WriteLine("INFORMATION: Test Button Clicked.");
MessagingCenter.Send("ISENGINEON", true.ToString());//Shared layer view models subscribe and react
}
- Add a subscriber anywhere you like. I use the constructor of a view model
MessagingCenter.Subscribe<string>(this,"ISENGINEON",OnEngineOnChanged);//Subscribe without caring who is sending the message. This is how we can hear from Android project without being able to reference MainActivity or a Broadcast receiver.
- Add the handler from the subscription
private void OnEngineOnChanged(string obj)
{
Console.WriteLine("INFORMATION> Well heck, it worked");
}
- Run the app
- Add breakpoints as desired for confirmation
Expected Behavior
When you click the button the message is sent - That is confirmed with breakpoints.
The subscriber should then run the assigned handler.
Actual Behavior
The handler is never executed. Indicating the subscriber never hears the message.