Skip to content

StackNavigationService

Mark Smith edited this page Aug 26, 2016 · 2 revisions

StackNavigationService

The StackNavigationService provides a simple implementation of the INavigationService interface by using the built-in Xamarin.Forms NavigationPage support.

Usage

Pages are identified with string-based keys - typically stored as constants in a class. You register a creator delegate function that returns a Page with the service and when the app requests to navigate to the given page-key, the delegate is called to return the page. That delegate could create a new page, return an existing page, or even cancel the navigation and perform some other action such as manipulate the MasterDetailPage.

To use this service, you must do several things:

  1. Register it as the implementation for INavigationService. This is typically done in the App constructor.
  2. Have a NavigationPage either as the MainPage, or as the first child of a MasterDetailPage.
  3. Register the pages you want to navigate to.
  4. Use the service to request navigation

Register the implementation

Here's an example of registering the service with the built-in DependencyService:

public class App
{
   public App()
   {
      DependencyService.Register<INavigationService,StackNavigationService>();
      ...
   }
}

Use a NavigationPage

The Application main page must be a NavigationPage, or you can use a MasterDetailPage where either the Master or Detail property is set to a NavigationPage. It checks Master first.

Register your pages and actions

You must register each unique page you want to navigate to with the service. This is commonly done in the App constructor. The RegisterPage method takes the string-based key and a delegate which will be called when a navigation is requested to that key - it must return a Page or null to cancel the navigation request and do nothing. If a Page is returned, then the class will use the Forms INavigation interface to navigate to the

public static class AppPages
{
   public const string Page1 = "Page1Key-Must-Be-Unique-String";
   public const string Page2 = "AnyTextWillDo-AsLongAsItsUnique;
   ...
}

public class App
{
   public App()
   {
      DependencyService.Register<INavigationService,StackNavigationService>();
      ...
      MasterDetailPage mdPage = ...;
  
      INavigationService navService = DependencyService.Get<INavigationService>();
      navService.RegisterPage(AppPages.Page1, () => new Page1());
      navService.RegisterPage(AppPages.Page2, () => { mdPage.IsPresented = false; return null; });
     ...
   }
}

Use the navigation service

Finally, in your VMs (or anywhere in your app) you can use the INavigationService to do the navigation using the registered page keys. You can also pass in an optional BindingContext for the page.

public class MyViewModel
{
   IDependencyService ds;

   async Task OnShowPage1Async() {
      // Use "SelectedItem" property value as the BindingContext for this new page.
      await ds.Get<INavigationService>().NavigateAsync(AppPages.Page1, SelectedItem);
   }
}

Clone this wiki locally