messaging center best practices

BenjaminTooleBenjaminToole USMember ✭✭✭

I've been looking around at how others are using messaging center. One common theme I have noticed is that you are supposed to subscribe when the page is appearing and unsubscribe when disappearing. The only time I need to use the messaging service is when i'm on another page. Doesn't that just defeat the purpose of a messaging service?

Best Answer

Answers

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    @BenjaminToole

    So when your 'other' page appears you subscribe. When your 'other' page disappears you unsubscribe. Then you'll only hear the messages when you're on the other page.

  • JohnHardmanJohnHardman GBUniversity mod
    edited March 2017

    @BenjaminToole - It really depends on what you are using the messaging center for, what is generating the messages, and on what thread. Given the order that OnDisappearing and OnAppearing are called, there may be circumstances where you do not want them to do your unsubscribe and subscribe.

  • BenjaminTooleBenjaminToole USMember ✭✭✭

    @ClintStLaurent said:
    @BenjaminToole

    So when your 'other' page appears you subscribe. When your 'other' page disappears you unsubscribe. Then you'll only hear the messages when you're on the other page.

    Say its a navigation page, the first page on the stack has a subscription (OnAppearing()), when another page is pushed on the stack (OnDisappearing()) the subscription stops listening and cannot receive any updates from the page pushed on the stack. How can I receive updates on the first page in the stack if the subscription stops when another page is pushed on the stack.

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    Why should it - is the bigger question? Its a page.. Its just UI - no logic.

    If its not the foreground page the user isn't seeing it anyway. Why would the page need to do anything when its not being seen? Its a waste of CPU. Its not like updating a... map... or label... on a page not being seen is going to do anything for the user.

    Unless you have logic taking place in your page code behind. At which point that would be the source of the problem; not the MessageCenter subscriptions.

  • BenjaminTooleBenjaminToole USMember ✭✭✭

    How am I supposed to update data from one page to another? If I leave a page and is unsubscribed it cannot receive any data

  • BenjaminTooleBenjaminToole USMember ✭✭✭

    Main Point: If I want to update data a couple viewModels back on the stack due to changes further up the stack, how can I do this?

  • NMackayNMackay GBInsider, University mod
    edited March 2017

    @BenjaminToole

    1. use message center
    2. Have a singleton service in your IoC container that gets injected into the viewmodel of each page, when you update the property/collection etc the pages further up the stack will update if they implement INPC.

    I use the 2nd approach when doing view/widget dicovery in prism module sin my app, as each module gets instantiated it registers the widget & menu navigation options in a module service, the dashboard UI elements bind to this same service and the binding takes care of itself.

    In your case maybe messagecenter might be the easiest way, my approach was the best way for my app.

  • 15mgm1515mgm15 USMember ✭✭✭✭

    @NMackay Do you have an example of your singleton service to have an idea on how to implement it?

    You see I am trying to find the best way to update data from other view models and the approaches that I have come up with are:

    1.- Having a static variable of the ViewModel so I can access all object from whenever I need to.
    2.- Pass the ViewModel as a Parameter of the Next Page.
    3.- Use the messaging center.

    I think your solution sounds very robust and like the correct way to do it so I was just wondering.

    Any comments on my solutions, please let me know.

  • SmeSme USMember ✭✭✭
    edited August 2017

    @15mgm15 said:
    @NMackay Do you have an example of your singleton service to have an idea on how to implement it?

    You see I am trying to find the best way to update data from other view models and the approaches that I have come up with are:

    1.- Having a static variable of the ViewModel so I can access all object from whenever I need to.
    2.- Pass the ViewModel as a Parameter of the Next Page.
    3.- Use the messaging center.

    I think your solution sounds very robust and like the correct way to do it so I was just wondering.

    Any comments on my solutions, please let me know.

    The way I'm doing it, is I have a singleton "Manager" class. For example, maybe I have a string that, when changed in one view model, needs to update another view model at the same time. In my singleton "Manager" class:

    private string _myString = "";
    
    public string MyString
    {
     get { return _myString; }
     set
     {
        _myString = value;
        OnPropertyChanged("MyString");
    }
    

    Then for my view models, both have a property that is a reference to that manager:

    public Manager MyManagerReference { get; set; }
    

    So now when I call MyManagerReference.MyString = "updated string";, the string will be updated in both view models.

    And of course, to bind the string to a view, just use Text={Binding MyManagerReference.MyString}

Sign In or Register to comment.