[Mvvm light] use of messenger

Source: Internet
Author: User

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);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.