This article has the goal to show how to create a navigation service for WPF application that uses Modern UI.
Modern UI is a set of controls and styles converting our WPF application into a great looking Modern UI app. The Modern UI project can be find in mui.codeplex.com, here is possible to get the WPF app that demostrate the features provided by "mui".
WPF don´t have a pattern for the navigation when we are using Windows, only exist a navigation service when we use Pages. ModernUI introduce a special way for the navigation, that use the ModernFrame.
For the sample we will use MVVMLight Toolkit for help in the MVVM pattern implementation and we will use a new feature provided by this toolkit, the INavigationService interface.
Note: The MVVMLight Toolkit don´t have any implementation of INavigationService for WPF, for see more about it read this article Announcing MVVM Light V5 for Windows and Xamarin.
The source code base, that we will used in this sample, is similar to the sample used in the article Modern UI for WPF application by example (Default Window).
We will start to create the interface IModernNavigationService and the NavigationService, after it we will configure the navigation in MainWindow and then will use the navigation service in the view model for navigate to another view.
The interface will be something like
public interface IModernNavigationService : INavigationService
{
/// <
summary
>
/// Gets the parameter.
/// </
value
/// The parameter.
object Parameter { get; }
}
public class NavigationService : IModernNavigationService
private readonly Dictionary<
string
, Uri> _pagesByKey;
private readonly List<
> _historic;
/// Initializes a new instance of the <
see
cref
=
"NavigationService"
/> class.
public NavigationService()
_pagesByKey = new Dictionary<
, Uri>();
_historic = new List<
>();
/// Gets the key corresponding to the currently displayed page.
/// The current page key.
public string CurrentPageKey
get;
private set;
public object Parameter { get; private set; }
/// The go back.
public void GoBack()
if (_historic.Count > 1)
_historic.RemoveAt(_historic.Count - 1);
NavigateTo(_historic.Last(), null);
/// The navigate to.
param
name
"pageKey"
/// The page key.
public void NavigateTo(string pageKey)
NavigateTo(pageKey, null);
"parameter"
public virtual void NavigateTo(string pageKey, object parameter)
lock (_pagesByKey)
if (!_pagesByKey.ContainsKey(pageKey))
throw new ArgumentException(string.Format("No such page: {0}. Did you forget to call NavigationService.Configure?", pageKey), "pageKey");
var frame = GetDescendantFromName(Application.Current.MainWindow, "ContentFrame") as ModernFrame;
// Set the frame source, which initiates navigation
if (frame != null)
frame.Source = _pagesByKey[pageKey];
Parameter = parameter;
_historic.Add(pageKey);
CurrentPageKey = pageKey;
/// Configures the specified key.
"key"
>The key.</
"pageType"
>Type of the page.</
public void Configure(string key, Uri pageType)
if (_pagesByKey.ContainsKey(key))
_pagesByKey[key] = pageType;
else
_pagesByKey.Add(key, pageType);
/// Gets the name of the descendant from.
"parent"
>The parent.</
"name"
>The name.</
returns
>The FrameworkElement.</
private static FrameworkElement GetDescendantFromName(DependencyObject parent, string name)
var count = VisualTreeHelper.GetChildrenCount(parent);
if (count < 1)
return null;
for (var i = 0; i < count; i++)
var frameworkElement = VisualTreeHelper.GetChild(parent, i) as FrameworkElement;
if (frameworkElement != null)
if (frameworkElement.Name == name)
return frameworkElement;
frameworkElement = GetDescendantFromName(frameworkElement, name);
private void SetupNavigation()
var navigationService = new NavigationService();
navigationService.Configure(ViewModelLocator.ResourcePageKey, new Uri("Views/ResourcesView.xaml"));
navigationService.Configure(ViewModelLocator.StepsPageKey, new Uri("Views/StepsView.xaml"));
SimpleIoc.Default.Register<
IModernNavigationService
>(() => navigationService);
public class StepsViewModel : ViewModelBase
private readonly IModernNavigationService _modernNavigationService;
"StepsViewModel"
"modernNavigationService"
/// The modern Navigation Service.
public StepsViewModel(IModernNavigationService modernNavigationService)
_modernNavigationService = modernNavigationService;
ResourcesCommand = new RelayCommand(ShowResources);
/// Gets or sets the resources command.
>The resources command.</
public ICommand ResourcesCommand { get; set; }
/// Shows the resources.
private void ShowResources()
_modernNavigationService.NavigateTo(ViewModelLocator.ResourcePageKey);
Note:
1. For send a parameter when navigate we should do
_modernNavigationService.NavigateTo(ViewModelLocator.ResourcePageKey, myParameterValue);
_modernNavigationService.Parameter
_modernNavigationService.GoBack();
See others solutions for Navigation in ModernUI applications. Modern UI for WPF application by example (handle navigation) Modern UI for WPF application by example ( NavigationMessageService - MVVM )
Get the source code for this sample in github.