Skip to content
This repository has been archived by the owner on Aug 27, 2020. It is now read-only.

INavigationService

Mark Smith edited this page Sep 2, 2016 · 4 revisions

INavigationService

The INavigationService provides a simple abstraction for a navigation service usable with Xamarin.Forms. This allows a View Model to drive navigation based on commands or property changes.

The pages are identified using object-based keys and must be registered in the app somewhere (typically in the App constructor).

The library provides a default implementation based on the Xamarin.Forms NavigationPage class in FormsNavigationPageService.

Events

  • Navigated : raised when a new page navigation (push) occurs.
  • NavigatedBack : raised when a backwards page navigation (pop) occurs.

Methods

  • NavigateAsync : navigates to a page specified by the string-based key. Can optionally provide the BindingContext.
  • CanGoBack : Returns true/false if a page is on the back-navigation stack.
  • GoBackAsync : Pops the first Page off the back-navigation stack and displays it.
  • PushModalAsync : Pushes a new page onto the modal stack using the page-based key. Can optionally provide the BindingContext.
  • PopModalAsync : Pops the first Page off the modal stack and displays it.
/// <summary>
/// Interface to manage navigation in the application.
/// </summary>
public interface INavigationService
{
    /// <summary>
    /// Event raised when NavigateAsync is used.
    /// </summary>
    event EventHandler Navigated;

    /// <summary>
    /// Event raised when a GoBackAsync operation occurs.
    /// </summary>
    event EventHandler NavigatedBack;

    /// <summary>
    /// Navigate to a page using the known key.
    /// </summary>
    /// <returns>The async.</returns>
    /// <param name="pageKey">Page key.</param>
    /// <param name="viewModel">View model.</param>
    Task NavigateAsync(object pageKey, object viewModel = null);

    /// <summary>
    /// Returns true/false whether we can go backwards on the Nav Stack.
    /// </summary>
    /// <value><c>true</c> if can go back; otherwise, <c>false</c>.</value>
    bool CanGoBack { get; }

    /// <summary>
    /// Pops the last page off the stack and navigates to it.
    /// </summary>
    /// <returns>Async response</returns>
    Task GoBackAsync();

    /// <summary>
    /// Push a page onto the modal stack.
    /// </summary>
    /// <returns>Async response</returns>
    /// <param name="pageKey">Page key.</param>
    /// <param name="viewModel">View model.</param>
    Task PushModalAsync(object pageKey, object viewModel = null);

    /// <summary>
    /// Pops the last page off the modal stack
    /// </summary>
    /// <returns>Async response</returns>
    Task PopModalAsync();
}