About MvvM Navigation and Pageflow - PeterBurke/MvvmCross-Forms GitHub Wiki

Navigation in MvvM is all about executing a command that moves you from one Page to another. In the simplest of situations there is a one to one relationship between Page classes and View Model classes and when you navigate your current Page and ViewModel are disposed and you move to a new Page and ViewModel.

Is there any situation where Page and ViewModel are not one to one, e.g. one ViewModel with multiple Pages to choose from? Well yes, one of us stefandevo recently added functionality to allow for loading a different View depending on the Device geometry.

How about one Page with many ViewModels? I don't think this need would ever arise.

So we have a situation where a Command wants to Navigate. Do we Navigate by providing the type of ViewModel or do we navigate by providing the type of Page?

All Xamarin XAML examples I have studied navigate by providing a Page type, and within the XAML there is configuration that loads the ViewModel and binds to it. Now you may think that since there can be many pages for one ViewModel this must be the best way to do that.

MvvMCross however uses Convention over Configuration in that given a ViewModel name called say "BingoViewModel" the associated Page class will be called "BingoPage". If there is more than one page associated with the ViewModel then we could extend the convention, e.g. we could have three pages: BingoPage_small, BingoPage, BingoPage_large and then let the infrastructure select the page using the Device class.

Modern practice recommends Convention over Configuration as it means less coding and higher productivity.

As an example take a look at the Xamarin XAML page below. I have highlighted where the XAML configures and binds to the Page.

XAML Page showing the configuration

If you were adapting that page to work with MvvmCross you must remove that configuration.

Another reason MvvmCross navigates to the ViewModel and not the Page is that often when you navigate you will want to initialise the second ViewModel based on data that has been captured and processed in the first ViewModel. MvvmCross contains infrastructure to do that, whereas things start to get very complicated if you try to do it via XAML configuration.