PRISM 101: Navigation
The best MVVM framework for Xamarin Forms :)… Honestly, don’t trust me, trust this guy
There is no way I can cover all that PRISM has to offer in a single blog post, so I’ll concentrate one of my favorite features: Navigation!.
This is what it feels like to handle navigation in a mobile app without using an MVVM framework
And this is how it feels when using the Navigation Service in PRISM
How does it work?
First you have to register all of your pages in the DI Container that you are using
Notice the RegisterTypes() method.
Now, if you embrace MVVM you’ll agree that navigation takes place in the ViewModel. We do this by injecting INavigationService in our constructor.
Then we can use _navigationService.NavigateAsync(“PageNameHere”) to load a page into the navigation stack or _navigationService.GoBackAsync() to pop a page from the stack. As simple as that!
What about parameters? Glad you asked
For this we have the NavigationParameters object, we can use this to pass along parameters, but be careful not to send too much information.
Now, how do we receive the parameters on the other end?
Simple, use use the same NavigationParameters object:
How do we know when a ViewModel is being navigated to, or you are about to navigate from a view model? Here is where INavigatedAware comes into play
By implementing this interface we have access to 3 new methods
This is where we can inject ourselves before the we navigate to the next Page.
This method is executed when another page navigates to us. This is called after the ViewModel has been pushed on to the stack.
Finally (this one is new in PRISM 6.3), the difference between this one and OnNavigatedTo is that this is called before the ViewModel has been pushed on to the stack.