Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
The ObservableRecipient type is a base class for observable objects that also acts as recipients for messages. This class is an extension of ObservableObject which also provides built-in support to use the IMessenger type.
Platform APIs:
ObservableRecipient,ObservableObject,IMessenger,WeakReferenceMessenger,IRecipient<TMessage>,PropertyChangedMessage<T>
How it works
The ObservableRecipient type is meant to be used as a base for viewmodels that also use the IMessenger features, as it provides built-in support for it. In particular:
- It has both a parameterless constructor and one that takes an
IMessengerinstance, to be used with dependency injection. It also exposes aMessengerproperty that can be used to send and receive messages in the viewmodel. If the parameterless constructor is used, theWeakReferenceMessenger.Defaultinstance will be assigned to theMessengerproperty. - It exposes an
IsActiveproperty to activate/deactivate the viewmodel. In this context, to "activate" means that a given viewmodel is marked as being in use, such that eg. it will start listening for registered messages, perform other setup operations, etc. There are two related methods,OnActivatedandOnDeactivated, that are invoked when the property changes value. By default,OnDeactivatedautomatically unregisters the current instance from all registered messages. For best results and to avoid memory leaks, it's recommended to useOnActivatedto register to messages, and to useOnDeactivatedto do cleanup operations. This pattern allows a viewmodel to be enabled/disabled multiple times, while being safe to collect without the risk of memory leaks every time it's deactivated. By default,OnActivatedwill automatically register all the message handlers defined through theIRecipient<TMessage>interface. - It exposes a
Broadcast<T>(T, T, string)method which sends aPropertyChangedMessage<T>message through theIMessengerinstance available from theMessengerproperty. This can be used to easily broadcast changes in the properties of a viewmodel without having to manually retrieve aMessengerinstance to use. This method is used by the overload of the variousSetPropertymethods, which have an additionalbool broadcastproperty to indicate whether or not to also send a message.
Here's an example of a viewmodel that receives LoggedInUserRequestMessage messages when active:
public class MyViewModel : ObservableRecipient, IRecipient<LoggedInUserRequestMessage>
{
public void Receive(LoggedInUserRequestMessage message)
{
// Handle the message here
}
}
In the example above, OnActivated automatically registers the instance as a recipient for LoggedInUserRequestMessage messages, using that method as the action to invoke. Using the IRecipient<TMessage> interface is not mandatory, and the registration can also be done manually (even using just an inline lambda expression):
public class MyViewModel : ObservableRecipient
{
protected override void OnActivated()
{
// Using a method group...
Messenger.Register<MyViewModel, LoggedInUserRequestMessage>(this, (r, m) => r.Receive(m));
// ...or a lambda expression
Messenger.Register<MyViewModel, LoggedInUserRequestMessage>(this, (r, m) =>
{
// Handle the message here
});
}
private void Receive(LoggedInUserRequestMessage message)
{
// Handle the message here
}
}
Examples
- Check out the sample app (for multiple UI frameworks) to see the MVVM Toolkit in action.
- You can also find more examples in the unit tests.
MVVM Toolkit