Address: http://jesseliberty.com/2011/01/06/windows-phone-from-scratch%E2%80%93mvvm-light-toolkit-soup-to-nuts-3/
This is the third part of the mvvm light series. Today, let's take a look at how messaging associates the view model with the view.
We will build:
To illustrate this, let's go back to the example created in the first part and expand the second part. We process the button click event on the page2 page.
We cancel the processing of this command and use viewmodel to complete the navigation from mainpage to page2. Unfortunately, the naveigationservice we need is not supported in viewmodel.
From viewmodelview to view should be invisible, so we need a mechanism to send a message like a bottle, and anyone interested can pick it up. Specifically, we want viewmodel to send a message indicating that it is time to navigate to page2. In addition, we need to register mainpage to receive the message, and call navigationservice at the receiving end to navigate to page2.
Messaging
Fortunately, mvvm light provides extensive support for messages. To achieve our goals, a simple process is required in three steps:
1. Create a class that contains the message to be passed.
2. In viewmodel, instantiate the message class and broadcast the message.
3. register the message in mainpage. XAML. CS and process it when it is received.
Create a new class in the project named gotopagemessage.
1: using System;
2:
3: namespace MvvmLightNavigationBehaviorAndMessages
4: {
5: public class GoToPageMessage
6: {
7: public string PageName { get; set; }
8: }
9: }
Return mainviewmodel. CS and remove the gotopage2 method. Create a gotopagemessage instance (initialize the Page name you want to navigate) and broadcast the message using the Messager object, as shown below:
1: private object GoToPage2()
2: {
3: var msg = new GoToPageMessage() { PageName = "Page2" };
4: Messenger.Default.Send<GoToPageMessage>( msg );
5: return null;
6: }
The following namespace must be referenced:
1: using GalaSoft.MvvmLight.Messaging;
In this way, the message is broadcast, and the rest is to register a recipient and respond to the message. To do this, return mainpage. XAML. CS and register the message in the constructor or mainpage_loaded function:
1: Messenger.Default.Register<GoToPageMessage>
2: (
3: this,
4: ( action ) => ReceiveMessage( action )
5: );
You need to add the messaging statement.
Receivemessage is a method you want to write to implement navigation.
1: private object ReceiveMessage( GoToPageMessage action )
2: {
3: StringBuilder sb = new StringBuilder( "/Views/" );
4: sb.Append( action.PageName );
5: sb.Append( ".xaml" );
6: NavigationService.Navigate(
7: new System.Uri( sb.ToString(),
8: System.UriKind.Relative ) );
9: return null;
10: }
Build and run the program. Click mainpage to navigate to page2.
So, is this easier?
No one thinks that what I show here is easier than simply handling click events in the code-behind file. What I want to talk about is that it is easier to test. The program looks at the viewmodel creation logic rather than processing it in code-behind.
In general, using behavior and message is much more complicated than this example. We will continue to discuss it in the future.
My understanding: here we just use navigation as an example to illustrate how to associate view with viewmodel. In fact, it is normal to directly write navigation events in view.