Sample that shows an easy pattern for MVVM-style navigation in Xamarin Apps
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.

How to Navigate Through a Xamarin.Forms App - Sample Implementation

Navigation in the MVVM architectural pattern seems a pretty tough task and most commonly found solutions don't really satisfy me. They are overly complex, involve lots of code or don't seem elegant at all. Some solutions just statically specify the target page as a string (in Windows/Windows Phone/UWP apps):


In Xamarin.Forms, navigation requires you to specify an instance of Page to navigate to. Usually, you would write code like this:

this.Navigation.Navigate(new SubPage());

This is bad for a couple of reasons:

  1. Navigation is a property of the Page type - you'll have to somehow pass this to your ViewModel or have some kind of glue in your code behind.
  2. Since we're calling the constructor of SubPage here, we're coupling these classes tightly. This means we cannot comfortable test the navigation portion in our ViewModel.
  3. Navigation without Pages is not possible - that means that unit tests would rely on the Xamarin.Forms framework, which they should not need to.

This repository contains a sample implementation of a pattern that solves these problems while still allowing easy configurability and short, elegant code. If you want to know more, read my blog post where I discuss the code in more detail.