Modern UI For WPF Application by Example (NavigationMessageService - MVVM): Part 3

Scope

The purpose of this article is to show how to create a navigation mensage service for a WPF application that uses the Modern UI.

Introduction

The Modern UI is a set of controls and styles that can make our WPF application a great looking Modern UI app. The Modern UI project can be found in mui.codeplex.com, where it is possible to get the WPF app that demostrates the features provided by the “mui”.

WPF doesn´t have a pattern for the navigation when we are using Windows, only a navigation service for Pages exists. The ModernUI introduces a special way for the navigation that uses the ModernFrame.

In the article Modern UI for WPF application by example (Handle Navigation: (Default)) we saw how to handle the navigation. In this sample we will see how to navigate among views using messages.

Description

The navigation we will implement is based on messages sent from a view model to the MainWindow, these messages are sent/received by the Messenger class provided by the MVVMLight Toolkit.

Start by creating the NavigationMessage as in the following:

  1. public class NavigationMessage  
  2. {  
  3.     /// <summary>  
  4.     /// Gets or sets the page.  
  5.     /// </summary>  
  6.     /// <value>The page.</value>  
  7.     public string Page { getset; }  
  8. }  
In the MainWindow register to receive the message defined as in the following:
  1. private void RegisterNavigation()  
  2. {  
  3.     Messenger.Default.Register<NavigationMessage>(this, p =>  
  4.     {  
  5.         var frame = GetDescendantFromName(this"ContentFrame"as ModernFrame;  
  6.             
  7.         // Set the frame source, which initiates navigation  
  8.         if (frame != null)  
  9.         {  
  10.             frame.Source = new Uri(p.Page, UriKind.Relative);  
  11.         }  
  12.     });  
  13. }  
Where GetDescendantFromName is the method that uses VisualTreeHelper to get the ModernFrame.

In the StepsViewModel sends the NavigationMessage with the path for the view we want to navigate.
  1. private void ShowResources()  
  2. {  
  3.     Messenger.Default.Send(new NavigationMessage()  
  4.     {  
  5.         Page = "Views/ResourcesControl.xaml"  
  6.     });   
  7. }  
Where we only navigated to the new view, to send the parameters. When navigating we should use another message with the parameter we want to send, with it the comunication will we among view models.

Source Code

Get the source code for this sample in github.

Visual Studio Extension used

 

<< Modern UI for WPF application by example (NavigationService - MVVM) Part-2

Up Next
    Ebook Download
    View all
    Learn
    View all