locked
messaging center best practices RRS feed

  • Question

  • User271155 posted

    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?

    Tuesday, February 28, 2017 10:13 PM

Answers

  • User76049 posted

    @BenjaminToole

    Also, unregister your message subscribers in the page Popped event and not OnDisappearing, that way your page further down the stack will get the messages and your message subscribers will only get cleaned up when the page is destroyed.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Wednesday, March 1, 2017 3:31 PM

All replies

  • User180523 posted

    @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.

    Wednesday, March 1, 2017 10:30 AM
  • User89714 posted

    @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.

    Wednesday, March 1, 2017 10:56 AM
  • User271155 posted

    @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.

    Wednesday, March 1, 2017 2:25 PM
  • User180523 posted

    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.

    Wednesday, March 1, 2017 2:53 PM
  • User271155 posted

    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

    Wednesday, March 1, 2017 3:12 PM
  • User271155 posted

    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?

    Wednesday, March 1, 2017 3:16 PM
  • User76049 posted

    @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.

    Wednesday, March 1, 2017 3:29 PM
  • User76049 posted

    @BenjaminToole

    Also, unregister your message subscribers in the page Popped event and not OnDisappearing, that way your page further down the stack will get the messages and your message subscribers will only get cleaned up when the page is destroyed.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Wednesday, March 1, 2017 3:31 PM
  • User57571 posted

    @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.

    Friday, August 4, 2017 5:42 PM
  • User251004 posted

    @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}

    Saturday, August 5, 2017 6:08 AM