When we use the mvvm development mode for development, communication between viewmodels is often a headache. Fortunately, mvvm light provides the messenger class to easily transmit messages between viewmodels.
Messenger
Other members of the messenger class can use the register method to establish contact with the messenger. The registration includes the method to be executed when the message is received. When the send method is used, the registered method is called.
The Messager is a class allowing objects to exchange message.
Main members:
Register Method:
// Registers a recipient for a type of message tmessage. The action
// Parameter will be executed when a corresponding message is sent.
Void register <tmessage> (Object recipient, object token, Action <tmessage> action );
Void register <tmessage> (Object recipient, Action <tmessage> action );
Void register <tmessage> (Object recipient, object token, bool receivederivedmessagestoo, Action <tmessage> action );
Void register <tmessage> (Object recipient, bool receivederivedmessagestoo, Action <tmessage> action );
Send Method
Example:
In the mainviewmodel class
Messenger. Default. register <brush> (this, true, M => backgroundbrush = m );
Indicates registering a message of the brush type.
In the settingviewmodel class
Messenger. Default. Send <brush, mainviewmodel> (
Getcurrentbrush ());
When you execute this sectionCodeWill call mainviewmodel to register the message written by M => backgroundbrush = m;
M indicates the message content.
The register function has the following structure:
void register (Object recipient, object token, Action action);