Silverlight-4.0 – MVVM and NavigationService

One of the many benefits of implementing any pattern is the separation of concerns between different layers in the application. In the case of Silverlight and MVVM, I think NavigationService belongs to UI.

If the NavigationService belongs to the UI, then it should be used in the XAML code behind, but the command occurs on the ViewModel. Should I raise an event on the Command in the ViewModel, let the View handle the event and call Navigation? ? If all I do is navigate to another page, that sounds ridiculous. Shouldn’t I handle UI events directly and navigate from there?

View Control Event -> ViewModel Command -> Raise Event -> View
Handled Event -> Navigation

or

View Control Event -> View Handled Event -> Navigation

this There are two ways to record the problem

>Use the message passing function of MVVM Light to achieve navigation. This method was proposed by Jesse Liberty in Part 3 of his MVVM Ligtht soup to nuts series. His method It is to send a message from the command to the view, indicating that the navigation operation should be performed.
>Implement a ViewService that handles navigation. This method is Laurent Bugnion’s response to Jesse’s post. This implements a service that handles the processing triggered by the view model. All navigation operations.

These two methods only involve navigation in WP7 applications. However, they can also be applied to Silverligt applications.

Jesse’s method is easier in SL Use, because it does not require access to the root visual. However, the navigation code is distributed in multiple places, and the following code is required to perform the actual navigation.

Laurent’s method requires access to the root visual – used to access the built-in navigation function As shown in Laurent’s code, accessing this point is no big deal in WP7 applications. However, in SL applications, since there is no surround frame, it is slightly more complicated. However, I implemented the SL pattern in one of my projects The necessary wiring was done with additional attributes-so although it requires more work, it can also be used for SL.

In a word-although, Jesse’s method is easier to implement, but I personally prefer Laurent’s method , Because of its clearer architecture-no code is required, and functions are encapsulated into a single component, so located at one point.

Achieve many benefits of any pattern One is to separate concerns between different layers in the application. In the case of Silverlight and MVVM, I think NavigationService belongs to UI.

If the NavigationService belongs to the UI, then it should be used in the following XAML code, but the command occurs on the ViewModel. Should I raise an event on the Command in the ViewModel, let the View handle the event and call Navigation? If all I do is navigate to another page, that sounds ridiculous. Shouldn’t I handle UI events directly and navigate from there?

View Control Event -> ViewModel Command -> Raise Event -> View
Handled Event -> Navigation

or

View Control Event -> View Handled Event -> Navigation

There are two ways to record this problem

> Use MVVM Light’s message passing function to achieve navigation. This method was proposed by Jesse Liberty in Part 3 of his MVVM Ligtht soup to the Nut series. His method is to send a message from the command to the view, indicating that navigation should be performed Operation.
>Implementing a ViewService that handles navigation is Laurent Bugnion’s response to Jesse’s post. This implements a service that handles all navigation operations triggered by the view model.

This Both methods only involve navigation in WP7 applications. However, they can also be applied to Silverligt applications.

Jesse’s method is easier to use in SL because it does not require access to the root vision. However, The navigation code is distributed in multiple places, and the following code is needed to perform the actual navigation.

Laurent’s method requires access to root visual-used to access the built-in navigation function. As shown in Laurent’s code, access to this point is No big deal in the WP7 application. However, in the SL application, since there is no surround frame, it is a little more complicated. However, I implemented the SL mode in one of my projects to do the necessary wiring with additional attributes-so although it needs More work, but it can also be used for SL.

All in all-although, Jesse’s method is easier to implement, but I personally prefer Laurent’s method because of its clearer architecture-no code required, And the function is encapsulated into a separate component, so it is located at one point.

Leave a Comment

Your email address will not be published.