MVVM Light Messenger

MVVM Light Messenger is a class that allows exchange messages between objects. Messenger class is mainly used for sending messages between viewmodels. Messenger class decreases coupling between viewmodels. Every viewmodel can communicate with another viewmodel without any association between them.

Messenger is an implementation of Mediator pattern in MVVM Light toolkit. You can know more about Mediator pattern here.

Messenger classes in MVVM Light Toolkit

IMessenger
Interface which all the Messenger classes must inherit. Some methods are Register, Send, and Unregister.

Messenger
Messenger class is the implementation of IMessenger interface. This class is used for send and receive messages between objects.

Messenger Class Important Properties and Methods

Reset (static) : voidDelete the default instance of Messenger. When you again use Default property of Messenger, it creates new Messenger class.

Register<TMessage>(object recipient, object token, bool receiveDerivedMessagesToo, Action<TMessage> action) : void
This method registers a recipient for a type of message TMessage. TMessage can be anything like int, string or custom class. MVVM Light toolkit also provides some classes which can be used for sending the message like NotificationMessage, PropertyChangedMessage<T> etc. I described these classes later in this section. ?
All four parameters usage are:

object recipient : ?The object which will receive the message. You can use "this" for registering current object for receiving messages or you can specify other objects.

object token : Token is a keyword for sending the message to those objects who register with that particular keyword. For example, if two recipients register with the "ViewModelA" token and "ViewModelB" token and message is send using "ViewModelA" token then only that object got the message that register with "ViewModelA" token.

bool receiveDerivedMessagesToo : If we set this flag to "true", then the recipient also gets message types which inherit from <TMessage>. For example, if bike and car both implement the interface IAutomobile, then registering with the type <IAutomobile> and set the receiveDerivedMessagesToo flag to true, allows recipient to get bike and car messages too.

Action<TMessage> action : The action delegate will be executed when message of <TMessage> is sent from the Send message.

Send<TMessage>(TMessage message, object token) : void
This method sends messages to registered recipients. Only those recipients will receive messages that register for that particular <TMessage>.?All two parameters usage are:

TMessage message : Message parameter is an instance of type TMessage.

object token : Token as explained earlier is a keyword for sending the messages to only those recipients that register for that particular keyword.?

Cleanup : void This method scans the recipients list for the dead recipients. All recipients are stores as WeakReferences, they can be claimed during the garbage collection process. Cleanup method removes those recipients that are collected by garbage collector.

Messages Classes in MVVM Light Toolkit

MessageBaseBase class for all messages types used by the Messenger for sending messages. There are only two properties exists in this class Sender and Target. You can only set both properties in the constructor. Both properties are of type object. Setting both properties are not necessary. These exist only for sending indications to recipients.

NotificationMessage<T>Use for sending "string" message notification and a generic value <T> to recipient(s).

NotificationMessageActionUse for sending "string" message notification with a callback Action. When the recipient processed the message, it can execute the callback Action.

NotificationMessageAction<TCallbackParameter>Use for sending "string" message notification with a callback Action with one <TCallbackParamter> parameter. When the recipient processed with message, it can execute the callback Action with one parameter.

PropertyChangedMessage<T>Use for sending the property name along with its old value and new value.

DialogMessage [deprecated]Use for requesting to display the messagebox. This class also provides a callback Action. This callback is used for notify the sender about the user's choice in the message box.

GenericMessage<T>Use for sending generic value to a recipient. Recipient can access the generic value using Content property.

You can create your own custom messages classes by inherit from the MessageBase class.

ViewModel support for Messenger

Every viewmodel inherits from ViewModelBase in MVVM Light Toolkit. ViewModelBase provides a MessengerInstance property which by default returns the default instance of Messenger.