In Xamarin Forms you access navigation via the NavigationPage. A single NavigationPage contains all the page you Push to the stack and you pop them out when done. Its works like exactly like a LIFO (Last In First Out) queue.
Its easy and simple but not if you want to keep any form of control over the Navigation Stack and create different stacks (as I will get into later in the series). Why do you want a centralized Navigation Service?
- Go to a page via a key (not a direct reference to a page)
- Control the Page Build process (an important step coming up later)
- Provide appropriate locks and controls over moving to a new page
- Provide Page Cleanup (will come up later as well)
If you want to see how I have implemented a Navigation Helper you can look at my ExtNavigationService.cs
The navigation service is simple:
- On construction give it the NavigationPage and IPageService (this is the page constructor service, I will talk about later).
- Map a string to a page type via the Map method.
- Push and Pop via the NavigateTo and GoBack methods.
The NavigationService then implements an interface and is registered in the IoC container for use where appropriate in your app. A list of string keys is normally kept in a file as constants, that should be easily accessible to the code that requests navigation commands as you can see in my PageLocator.cs class.