MVVM Light Messaging
Posted on July 5, 2010 by Chris Koenig
Since I started using the MVVM Light toolkit, I’ve gotten lots of questions about the it. In this post, I’m going to show you the basics of using the Messaging infrastructure. In the sample application attached, we have the situation where a Master page with a list of data items is trying to move to the Details page to show the details for one specific item. There are a couple of ways to do this, but the one I’ve starting using is the Messaging infrastructure in MVVM Light. It involves the ViewModels communicating with each other via a lightly-coupled messaging bus rather than a direct coupling or using the query string. Let’s break it down…
The sample application displays a fixed set of data elements in a list on the main page. When an item is selected from the list, it navigates to the details page and shows the details. Presuming you already have the main page working, here’s what’s left:
Step 1 – Send the message out to the bus
When you create a new ViewModel property using the MVVM Light snippets, the sample code that gets created asks you to make a decision between publishing the PropertyChanged event to the message bus or not. In our case, we want to publish to the message bus. This will allow registered receivers to pick up this message. So – inside the MainViewModel, we have this property:
public Person SelectedItem
{
get
{
return _selectedItem;
}
set
{
if (_selectedItem == value)
{
return;
}
var oldValue = _selectedItem;
_selectedItem = value;
// Update bindings and broadcast change using GalaSoft.MvvmLight.Messenging
RaisePropertyChanged(SelectedItemPropertyName, oldValue, value, true);
}
}
The RaisePropertyChanged method includes two overrides – this one publishes to the Messaging infrastructure for us. Now all we need is a listener…
Step 2 – Register the Details view model to listen for the appropriate message
Now that the message is getting published to the message bus, we need a receiver to pick it up and act on it. In our case, that’s the DetailsViewModel. In the constructor for the ViewModel, we add the necessary code to register for receiving SelectedItem PropertyChanged messages like this:
public DetailsViewModel()
{
Messenger.Default.Register<PropertyChangedMessage<Person>>(
this,
(action) => DispatcherHelper.CheckBeginInvokeOnUI( () => this.Item = action.NewValue )
);
}
Notice the Register statement – we need to register for instances of the PropertyChangedMessage message that transport Person objects. Our example is simple, so any type a PropertyChanged message is sent into the bus with a Person attached to it, the DetailsViewModel will pick it up. If there is any disambiguation that needs to be made, there are a couple of other overrides to the Register method that allow you to provide a “token” (i.e. message name) as well as a switch to manage whether or not derived messages will also be picked up,
Since the GlobalViewModel locator creates and manages the lifetimes of our ViewModels, the DetailsViewModel is always instantiated and is available at the beginning of the application lifecycle to register for these messages. When the messages arrive, even if we don’t navigate to the Details page, the DetailsViewModel is ready for us.
The full sample can be downloaded right here.






Pingback: links for 2011-01-12 - Craig's Blog
Pingback: Simple MVVM Toolkit versus MVVM Light Toolkit | Tony Sneed's Blog