Forum Xamarin Xamarin.Forms

Messaging Center called twice in xamarin forms

HarshitaHarshita INMember ✭✭✭✭
edited February 2018 in Xamarin.Forms
MessagingCenter.Subscribe<string>(this, "LocationName", (value) =>
                    FullPathName.Text = value;

While I set a break point and notice that This MessagingCenter.Subscribe called twice.
How to solve this?


  • JamesLaveryJamesLavery GBBeta, University ✭✭✭✭✭
    Where are you calling it? We need more context.
  • JohnHardmanJohnHardman GBUniversity admin
    edited February 2018

    @Harshita - Add some trace statements as below, and let us know how many times you see Position 1 and Position 2 in your Output window.

                    System.Diagnostics.Debug.WriteLine("Position 1");
                    MessagingCenter.Subscribe<string>(this, "LocationName", (value) =>
                        System.Diagnostics.Debug.WriteLine("Position 2");
                        FullPathName.Text = value;

    If you see Position 1 more than once, you have a bug in your code that results in you subscribing more than once.

    If you only see one Position 1, but two of Position 2, then look at where you are doing the MessagingCenter.Send to see if you are doing that more than once. If you are, then that's a bug in your code assuming you only want to be calling it once.

    However, if you only subscribe once, and only send once, but see Position 2 more than once, you may be seeing something I encountered yesterday (using XF 2.4.0) on iOS. I received two notifications, even though I only did one subscribe and one send. I didn't actually need to be using MessagingCenter, so I replaced that code by something else. However, it looked to me as if there is a XF bug lurking in 2.4.0, but rule out problems in your own code before assuming it's a XF problem.

  • HarshitaHarshita INMember ✭✭✭✭

    I am calling It from a content page button click,and after sending the data,receive it inside constructor.

  • CharwakaCharwaka INMember ✭✭✭✭✭

    Hi @Harshita

    may be your calling or subscribed twice , if your pretty sure that subscribed only once, than check some where in your project you might be calling the below message twice

    MessagingCenter.Send<MainPage, string> (this, "LocationName", "yourmessage");

  • SudharsanCSudharsanC INMember ✭✭

    Me too having the same problem, that messaging center from PCL to Native platform called twice when it is running. Actually i have to start some background service from PCL to Native platform. After calling Messaging center's Send the subscribe called twice the time.

  • JamesLaveryJamesLavery GBBeta, University ✭✭✭✭✭
    See John's comment above. You almost definitely are subscribing twice or registering an event handler twice.
  • MrDaneeyulMrDaneeyul Member ✭✭

    Just ran into this issue myself! I think I've fixed it. Are you subscribing to the message in your constructor?

    Does your MessagingCenter.Send create a new version of the recipient when it sends? For example, the LoginPage is the subscriber to the message in this case:

    MessagingCenter.Send(new LoginPage(), "MessageTitle", userID);

    What happens here (as far as I can tell) is that when the .Send creates the new instance of the subscriber, it calls the constructor, which then subscribes a second time. You can resolve this by either passing in the existing instance of the subscriber in Send, OR move the subscribe somewhere else (OnAppearing is a good place to subscribe anyway).

    Hope this helps :)

  • I am having this issue on a detail page which is backed by a view model that subscribes to messages in its constructor. In the detail I have messages to add and remove children as well as a button to remove the parent item itself. Every time I leave my detail page and go back to it I re-subscribe to all the message. I have caused the same message to be handled 3 times just by closing my detail and going back to it. I guess if I do it right I can make the same message handled as many times as I like.

    I think a good solution would be to write a procedure in the view model that will unsubscribe to all messages which can be called from the view's xaml.cs code before I pop the page off the stack. That should work but this seems like a bug in the framework. Or, the out of the box master/detail project should implement some unsubscribe functionality somewhere in the code so we know it has to be done.

    Maybe there is a method I should be overloading or an event I should be handling but I cannot find it...

  • PieterjanDeClippelPieterjanDeClippel USMember ✭✭
    edited December 2018

    I experience this issue when subscribing in the constructor of a page and unsubscribing in the destructor.
    Somehow, when a page is popped of the stack, the destructor is not being called and the page keeps being subscribed for the message. It's only way later in the app use that the page is being destroyed.
    Subscribing in the OnAppearing/OnDisappearing won't work since a page also disappears when another page comes upon it...
    Very annoying

  • ShantimohanElchuriShantimohanElchuri USMember ✭✭✭✭✭

    In a TabbedPage with two tabs I too noticed that the MessageCenter was being called twice. Hence I stopped subscribing and using other events to do the same thing.

    I have no idea what is the purpose of using MessageCenter? I don't find any advantage of using it.

  • CharwakaCharwaka INMember ✭✭✭✭✭
    Hi @ShantimohanElchuri @PieterjanDeClippel

    This issue was fixed in latest xamarin forms 3.4 so now onwards onappearing won't call twice.please try and tell me
  • ArselanArselan Member ✭✭

    I solved this problem by unsubscribing from MessagingCenter inside the OnDisappearing() method.

    `protected override void OnDisappearing()
    MessagingCenter.Unsubscribe<Your_Page, string>(this, "something");


Sign In or Register to comment.