Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

Anonymous MessageCenter messages aren't heard anymore #5809

Closed
tlhintoq opened this issue Apr 4, 2019 · 3 comments
Closed

Anonymous MessageCenter messages aren't heard anymore #5809

tlhintoq opened this issue Apr 4, 2019 · 3 comments
Labels
i/low Has trivial workaround; affects very few users i/regression s/needs-repro ❔ This reported issue doesn't include a sample project reproducing the issue. Please provide one. s/unverified New report that has yet to be verified t/bug 🐛

Comments

@tlhintoq
Copy link

tlhintoq commented Apr 4, 2019

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

  1. 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" />
  1. 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
       }
  1. 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.
  1. Add the handler from the subscription
        private void OnEngineOnChanged(string obj)
        {
            Console.WriteLine("INFORMATION> Well heck, it worked");
        }
  1. Run the app
  2. 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.

Basic Information

  • Version with issue:
  • Last known good version:
  • IDE:
  • Platform Target Frameworks:
    • iOS:
    • Android:
    • UWP:
  • Android Support Library Version:
  • Nuget Packages:
  • Affected Devices:

Screenshots

Reproduction Link

@tlhintoq
Copy link
Author

tlhintoq commented Apr 4, 2019

Further input from the community found a solution: You have to specify the type Application and Application.Current.
https://forums.xamarin.com/discussion/comment/370364#Comment_370364

So the previously working syntax seems broken in 2019 - there is a way around it.

Android Broadcast listener:
MessagingCenter.Send(Xamarin.Forms.Application.Current,"ISENGINEON", result.ToString());
Shared layer view model
MessagingCenter.Subscribe<Application,string>(this,"ISENGINEON",OnEngineOnChanged);

@samhouts samhouts added i/regression i/low Has trivial workaround; affects very few users s/unverified New report that has yet to be verified labels Apr 4, 2019
@kingces95
Copy link
Contributor

@tlhintoq Would you mind attaching a zipped reproduction of the issue? That really helps us triage. I'll cook one up if need be after triage of other issues with attached reproductions if you don't have the reduction handy.

@kingces95 kingces95 added the s/needs-repro ❔ This reported issue doesn't include a sample project reproducing the issue. Please provide one. label May 4, 2019
@samhouts samhouts added this to the 3.6.0 milestone May 29, 2019
@samhouts samhouts removed this from the 3.6.0 milestone Jun 21, 2019
@samhouts
Copy link
Member

@tlhintoq Since we haven't heard from you in more than 30 days, we hope this issue is no longer affecting you. If it is, please reopen this issue and provide the requested information so that we can look into it further. Thank you!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
i/low Has trivial workaround; affects very few users i/regression s/needs-repro ❔ This reported issue doesn't include a sample project reproducing the issue. Please provide one. s/unverified New report that has yet to be verified t/bug 🐛
Projects
None yet
Development

No branches or pull requests

3 participants