Skip to content

MVVM Navigator

Reflection Emit edited this page Dec 15, 2017 · 9 revisions

Navigator

The Navigator handles the creation of new windows in a non-single-page application. This also includes the creation of modal pop-up that can return results. A viewmodel that is meant to return result has to implement the IDialogViewModel interface.
The Navigator will also take care of creating the associated view of the viewmodel. There are several ways to associate a view to a viewmodel.

  • Using the View attribute - You can directly specify in the ViewModel which view is to use. The ViewAttribute accepts System.Type and also contract names defined by ComponentAttribute.
  • Naming Convention - The viewmodel has to have the ViewModel suffix, while the View has the View suffix.
    Examples: MyViewModel -> MyView, MainViewModel -> MainView, SupplierSelectionViewModel -> SupplierSelectionView
  • DataTemplate - The Navigator uses the CauldronTemplateSelector to select the datatemplate for the viewmodel. The datatemplate key has to follow the following naming convention: View_[ViewModelName]
    Examples: View_MyViewModel, View_MainViewModel, View_SupplierSelectionViewModel

Usage example

this.Navigator.NavigateAsync<MainViewModel>();

or

this.Navigator.NavigateAsync(typeof(MainViewModel));

or

this.Navigator.NavigateAsync(Assemblies.GetTypeFromName("MainViewModel"));

The navigator also accepts parameters:

this.Navigator.NavigateAsync<MainViewModel>("A parameter", 42);

Sample project

Win32_WPF_Navigator.csproj

Navigator - SinglePage

If the applciation's IsSinglePage property is set to true, a more specialized navigator is used; the NavigatorSinglePage class.
The navigator will be interacting with the NavigationFrame to handle the viewmodels. If a new viewmodel is loaded the previous viewmodel is disposed, but its type and call arguments are cache. This will be used if the user wants to go back to the previous page.