Implementing Navigation Services in MVVM for Xamarin

This technique uses ViewModel first navigation which makes all the sense in the world if you are using MVVM. Otherwise, not so much. See: https://docs.microsoft.com/en-us/xamarin/xamarin-forms/enterprise-application-patterns/ for more details.

Steps

  1. Make the following folders in the .NET Standard project:
    1. Services
    2. TinyIoC
    3. ViewModels

    4. ViewModels/Base
    5. Views
  2. This project requires the TinyIoC project (don't get it from nuget, just include this file in the TinyIoC folder.

  3. In Services create the following Files and code:
    •    1             public interface INavigationService
         2             {
         3                 ViewModelBase PreviousPageViewModel { get; }
         4                 Task InitializeAsync();
         5                 Task NavigateToAsync<TViewModel>() where TViewModel : ViewModelBase;
         6                 Task NavigateToAsync<TViewModel>(object parameter) where TViewModel : ViewModelBase;
         7                 Task RemoveLastFromBackStackAsync();
         8                 Task RemoveBackStackAsync();
         9             }