Message notification using mvvmlight

Source: Internet
Author: User

Read my mvvmlight tutorialsMvvmlight Design Model Series

As we mentioned in the article, the essence of mvvmlight is the Message notification mechanism, which is well designed. This is frequently used in mvvmlight. When viewmodel requests a view that needs to be changed (for example, a window is displayed), do you want to write the code for the pop-up window in viewmodel? This violates the mvvm design model. mvvmlight message notifications can call code without coupling!

 

How to Use mvvmlight Message notification

The project in our previous article also said that we should modify the shortcomings in the project to make the code elegant. So far, we have two forms (MainWindow,UserView), A viewmodel (UserViewModel). We are usingMainWindowPop-upUserViewIs written directly.MainWindowInButtonOfClickEvent. In this wayMainWindowMaster the business logic, and when should the logic pop up properly?UserViewIt should be determined by the person who compiled the viewmodel. Therefore, we should hand over the right of this pop-up window to viewmodel.

Maybe you want to write the following code in viewmodel now?
  1. Userview UV = new userview ();
  2. UV. Show ()

But write like this, right ?... If the view writer has not compiledUserViewWhat about this class? Is it still not decoupled? There is still such dependency,If view depends on viewmodel, or viewmodel depends on view, how can this problem be solved?

 

Next let's take a look at the solution in mvvmlight-Message notification

Viewmodel is a class that can grasp the business logic,So let's broadcast a message here, the idea! I am talking about broadcast! I don't want to specify who the message is sent..

I used the following code in userviewmodel. CS to broadcast messages.

  1. Messenger. Default. Send <Object> (null, "showuserview ");

This message sends a broadcast, and the broadcast token is"ShowUserView", This Is A Token! Just like a whisper, haha! The message can be received as long as it is correct, so we agree with the recipient (the engineer who compiled the view. This token is used to receive messages.

The parameters broadcasted here are of the object type. Because I do not need to transmit any parameters, I set the send <t> generic to object, the parameter value is null (that is, the first parameter ).

 

Receive mvvmlight messages

Agreed on a token (here is“ShowUserView”), I am registering this token here. I will receive this notification when there is a message with this token. Let's see how we registered the message in the view and used it!MainWindow.csThe Message notification code is as follows!

  1. Public mainwindow ()
  2. {
  3. This. datacontext = new mainwindowviewmodel ();
  4. Initializecomponent ();
  5. // Register the mvvmlight message
  6. Messenger. Default. register <Object> (this, "showuserview", showuserview );
  7.  
  8. // Uninstall all mvvmlight messages registered by the current (this) object
  9. This. Unloaded + = (sender, e) => messenger. Default. unregister (this );
  10. }
  11.  
  12. // Pop up the userview form
  13. Void showuserview (Object OBJ)
  14. {
  15. New userview (). Show ();
  16. }

Let's take a look at the line of code that registers the mvvmlight message. register <t> here is a generic type, which is the same as what we agreed. I gave the object type, therefore, when constructing a method, we also need to have a parameter method of the object type.ShowUserView(object obj).

OK. Continue to check the three parameters after this line of code.

First:thisIndicates the object for registering the message, that is, the message recipient. Therefore, I enter the current form.

Second:"ShowUserView"It is the token. It is agreed with the editor of viewmodel.

Third: the method to be executed when receiving the message. Here we registerShowUserView(object obj)This method.

This is perfect! No one depends on anyone! When will the form be displayed, and the business logic should be handed over to the viewmodel editor. As for the pop-up form, the form is more beautiful, and how to set the form, this is to write the view.

 

There seems to be another story ..... Uninstall message?

If you do not cancel a registration, the registration will always exist. If you open mainwinodw twice, it will be registered twice .. If both forms are open, then four userview forms will pop up when a message is received .. The reason is very simple because the two userview forms are registered twice... Each form is received twice ..

So we can cancel message acceptance when closing the form or when you need to stop receiving messages...At this point, you should understand that the message registration mechanism of mvvmlight is a static variable that is broadcast and registered globally in the app. There are indeed some troubles, but sometimes it is very convenient. There will be no previous use of multiple form-based transmission objects.

So here I amUnloadedThe message is logged out. Let's take a look at the code for cancellation.

  1. Messenger. Default. unregister (this );

This is to deregister all messages of the current object. If you want to deregister the specified message, there is a heavy load. You can specify the token name, such as "shwousreview". Please try again by tapping the code! As shown below!

  1. Messenger. Default. unregister <Object> (this, "showuserview ");

 

Download the sample source code in this article: Mvvmlightdemo_4

 

Now, the mvvmlight message notifications are almost the same. If you have any questions or other suggestions... Please reply here for discussion!

Read my mvvmlight tutorialsI will add some content related to mvvmlight IN THE mvvmlight design patterns series.

Reprinted Please note: Wang Xu blog» use mvvmlight Message notification

Continue to browse articles about message notifications of C # messengermvvmmvvmlight using mvvmlight

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.