Mvvm light v3: simple use of icationicationmessage and dialogmessage

Source: Internet
Author: User

The message type tree of the entire mvvm light may be a bit confusing, but it doesn't matter. The type function can be easily identified from the name. The sub-classes of messagebase are:

 

The notificationmessage type defines only one notification attribute, and the string type represents the passed string information. Its subclass: icationicationmessagewithcallback adds a callback function, that is, the delegate in C #. Then, through the Execute function, the receiver can call the corresponding delegate method. The next two sub-classes, icationicationmessageaction and notificationmessageaction <tcallbackparameter>, define the notificationmessage of the strongly delegated type, which are action and action <t>, in addition, execute also changes with the delegate type. If you need to customize other delegate types, you can choose to inherit the icationicationmessage type or directly use icationicationmessage to pass in the delegate object.

 

The second is the genericmessage <t> type, which defines a content attribute and the type is T. It is the parent class of the commonly used dialog message dialogmessage. The usage of dialogmessage is similar to that of notificationmessagewithcallback, and there is a callback method. Another type is icationicationmessage <t>. Note that it inherits from genericmessage <t> instead of icationicationmessage, but it is added to the member notification attribute of icationicationmessage. The type tree is a bit messy, but fortunately there are not many types and it is quite understandable.

 

The next step is dialogmessage, which contains common attributes of the create dialog box. These attributes are of the same type as those in the dialog box in WPF. Therefore, it is best to add the system. Windows namespace when using them:

 

For example, we can create an attribute in viewmodel: Text and an actioncommand. The command can be executed only when the text is "error" or "Ask. When text is error, the error dialog box is displayed. When text is ask, the question dialog box is displayed, and the user's selection is reported to the text attribute.

 

The Code executed by actioncommand first judges the text value, creates the dialogmessage object, and sends the dialogmessage through Messenger. Send. Note: In the ask option, initialize dialogmessage and pass in the lambda that sets the text attribute. In this way, when MessageBox is displayed, the corresponding delegate will be called.

Dialogmessage dlgmsg = NULL;

If (text = "error ")

{

Dlgmsg = new dialogmessage ("error", null );

Dlgmsg. Button = messageboxbutton. OK;

Dlgmsg. Caption = "error ";

Dlgmsg. Icon = messageboximage. error;

}

Else if (text = "Ask ")

{

Dlgmsg = new dialogmessage ("select", Res => text = string. Format ("You selected: {0}", Res ));

Dlgmsg. Button = messageboxbutton. yesno;

Dlgmsg. Caption = "problem ";

Dlgmsg. Icon = messageboximage. question;

}

 

If (dlgmsg! = NULL)

Messenger. Default. Send (dlgmsg );

 

In the code behind the view, register the dialogmessage message and directly use the dialogmessage attribute display dialog box. Finally, do not forget to call the result back and forth through the processcallback method of dialogmessage, if the delegate is null, it is not called.

 

Code:

Messenger. Default. register <dialogmessage> (this, dlgmsg =>

{

VaR res = MessageBox. Show (dlgmsg. Content, dlgmsg. Caption, dlgmsg. Button, dlgmsg. Icon );

Dlgmsg. processcallback (RES );

});

 

Run the program, enter ask, and click the button to run the internal relaycommand. The select dialog box is displayed:

 

For example, if no is selected, the result is fed back to the attributes in viewmodel:

 

The dialogmessage and notificationmessagewithcallback mentioned above are similar. Therefore, the above logic can also be completed using icationicationmessagewithcallback. Of course, the internal Delegate of icationmessmessagewithcallback cannot be null. Therefore, if the text is an error, you need to input a Lambda without any operation.

 

Write the code in viewmodel as follows:

Icationicationmessageaction <string> MSG = NULL;

 

If (text = "Ask ")

{

MSG = new notifmessmessageaction <string> ("Ask", Res => text = string. Format ("You selected: {0}", Res ));

}

Else if (text = "error ")

{

MSG = new notificationmessageaction <string> ("error", _ => {});

}

 

If (MSG! = NULL)

Messenger. Default. Send (MSG );

 

You can process icationicationmessageaction in the code behind the View:

Messenger. Default. register <icationicationmessageaction <string> (this, MSG =>

{

If (msg. Notification = "Ask ")

MSG. Execute (MessageBox. Show ("select", "problem", messageboxbutton. yesno, messageboximage. Question). tostring ());

Else if (msg. Notification = "error ")

MessageBox. Show ("error", "error", messageboxbutton. OK, messageboximage. Error );

});

 

 

The result will be the same as above. Of course, this is only the most basic example of using mvvm light Internal messages. You can extend more functions by inheriting these types, rather than simply relying on one notification string attribute in the above example.

 

 

Source code download

Note: This is an archive of Microsoft SkyDrive. Please download it directly in your browser. Some download tools may not be available for download.
Source code environment: Microsoft Visual C #2010 Express

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.