Xamarin Forms – View Model First Navigation

Xamarin Forms – View Model First Navigation

I recently started a brand spanking new Xamarin Forms project and I wanted to have the flow of the app run through the view model layer. In other words I wanted the currently active view model to determine which other view model should be loaded next, and then have the app display the correct associated view … all without the view model layer knowing anything about the views. The only problem … there isn’t a way to perform this “view model first” navigation in Xamarin Forms out of the box. Naturally my next step was off to Google to see what I could find. I came across a couple of things, but nothing that suited my needs 100%. So I took my experiences with MVVMCross (which has a very nice implementation of view model first navigation), ReactiveUI and an app built with ReactiveUI called CodeHub, and a great article by Jonathan Yates in his Adventure in Xamarin Forms series … mashed them all together and came up with the following view model first framework for Xamarin Forms. I can’t say with 100% certainty this is going to work in every situation – but so far it’s working well in my project with TabbedPage and NavigationPage’s. If you have any comments one way or the other – or you decide to use it, let me know what you think! (I just found that Xamarin’s own James Clancey has his own version of view model first navigation, and overall it’s not too far from what I have … so I must be going down the right path)! INavigationService In order to...