MessagingCenter called twice time

Hi
I am using MessagingCenter for sending some data from one page to other but MessagingCenter.Subscribe run twice time.
my code is
Send=>
MessagingCenter.Send(new myclass()
{
Id=2
}, "PopUpvalue");

received Value=>
MessagingCenter.Subscribe(this, "PopUpvalue", (value) =>{ //this is run twice time
var id= value.Id
});

thanks

Answers

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    We use MessageCenter extensively in our app. Most folks do.
    Message center doesn't send twice and if there were a bug with this in the eco-system you'd see thousands of developers screaming about it since so many people use it A LOT.

    So the bug is in your code. You're either sending twice, or you have two instances of the subscribing object and so both are hearing the same message. IE: If the Supervisor yells and you have 2 Employee object, they will both hear the message.

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    where do you subscribe (you should do in OnAppearing) and where do you usubscribe (you should do in OnDisappearing)?

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    @AlessandroCaliaro said:
    where do you subscribe (you should do in OnAppearing) and where do you usubscribe (you should do in OnDisappearing)?

    Assuming this is in UI code behind a view. It might be in a ViewModel.

  • JarvanJarvan Member, Xamarin Team Xamurai

    What method did you use for subscribing? Subscribe<TSender,TArgs> or Subscribe<TSender>?
    If you used MessagingCenter.Subscribe<TSender,TArgs>, don't forgot to add object when unsubscribing.

    MessagingCenter.Unsubscribe<XXX,object>(this, "string");  
    
  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    @ClintStLaurent said:

    @AlessandroCaliaro said:
    where do you subscribe (you should do in OnAppearing) and where do you usubscribe (you should do in OnDisappearing)?

    Assuming this is in UI code behind a view. It might be in a ViewModel.

    I use https://github.com/davidbritch/behaviors to have OnAppearing in my ViewModel

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭
    edited June 2019

    @AlessandroCaliaro said:
    I use https://github.com/davidbritch/behaviors to have OnAppearing in my ViewModel

    So if a view appears 8 times... lets say you have 8 EmployeeControl or 5 COMportControl on a page, then your ViewModel gets the message 8 times? Or if you're working a UWP solution where you might have two windows open (one on each monitor) then you see two events? Or any two pages with different layouts but using the same ViewModel?

    It just rubs me the wrong way thinking the ViewModel has any knowledge or concept of a view at all. But that's me. There's lots of right ways to do things.

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    OnAppearing fires when you popup a page, so you should have only one page appeared and only one Subscription (this in a normal situation, I have never used UWP so I don't know if there are some problems for this)

Sign In or Register to comment.