• Home
  • Documentation
Show / Hide Table of Contents
  • Introduction
  • Commands
    • Commanding
    • Composite Commands
    • Async Commands
    • Error Handling
  • Dependency Injection
    • Getting Started
    • Registering Types
    • Microsoft Extensions (Supplement)
    • Platform Specific Services
    • Exception Handling
    • ContainerLocator
    • Appendix
  • Dialog Service
    • Dialog Service
    • IDialogAware ViewModels
    • IDialogWindow (WPF & Uno Platform)
  • Event Aggregator
  • Mvvm
    • BindableBase
    • ViewModelLocator
  • Modularity
    • Getting Started
    • Module Catalog
    • Module Initialization
  • Navigation
    • Getting Started
    • INavigationParameters
    • Page Navigation
    • Regions
      • Getting Started
      • Region Manager
      • Region Adapters
      • Region Behaviors
      • About Navigation in Prism
      • Basic Region Navigation
      • View/ViewModel Participation
      • Navigating to Existing Views
      • Passing Parameters
      • Confirming Navigation
      • Controlling View Lifetime
      • Navigation Journal
  • Platforms
    • Maui
      • Getting Started
      • Migrating from Prism.Forms
      • PrismAppBuilder
      • AppModel
        • IPageLifecycleAware
      • Behaviors
        • Introduction
        • BehaviorBase<T>
        • EventToCommandBehavior
        • PageBehaviorFactory
      • Dialogs
        • Getting Started
        • IPageDialogService
      • Navigation
        • Introduction
        • Page Navigation
        • NavigationBuilder
        • TabbedPages
        • Understanding the INavigationResult
        • NavigationExceptions
        • Global Navigation Observer
        • XAML Navigation
    • Uno Platform
      • Getting Started
      • Uno.Extensions
    • Wpf
      • Introduction
      • Getting Started
      • View Composition
      • Interactivity
        • Event To Command
    • Xamarin.Forms
      • Create Your First App
      • Behaviors
        • Working with Behaviors
        • EventToCommand Behavior
        • PageBehaviorFactory
      • Dialogs
        • Dialogs
        • Page Dialog Service
        • Dialog Service
        • Styling Dialogs
      • Navigation
        • Navigation Basics
        • Passing Parameters
        • Confirming Navigation
        • Deep Linking
        • Working w/ MasterDetailPages
        • Working w/ NavigationPages
        • Working w/ TabbedPages
        • XAML Navigation
      • Application Lifecycle
      • Page Lifecycle
      • Additional Platforms
        • GTK
  • Magician
    • Getting Started
  • Plugins
    • Essentials
      • Getting Started
      • ApplicationModel
        • App Context
        • Browser
        • Communication
          • Email
          • Phone Dialer
        • Data Transfer
          • Clipboard
          • Share
        • LatestVersion
        • Launcher
        • Version Tracking
      • Devices
        • Battery
        • Sensors
          • Biometrics
          • Geocoding
          • Geofencing
          • Geolocation
      • IO
        • File System
        • Stores
      • Media
        • Getting Started
        • Camera
        • Video
      • Networking
        • Connectivity
      • Notifications
        • Getting Started
        • ActionSheets
        • Alerts
        • Prompts
      • Permissions
        • Permissions Manager
      • Threading
        • Main Thread
    • Logging
      • Getting Started
      • Interop
        • Essentials
        • Microsoft Extensions
      • Providers
        • AppCenter
        • Console
        • Debug
        • Firebase
        • Graylog
        • Kochava
        • Raygun
        • Sentry
        • Unit Testing
        • Xunit
    • Observable Regions
    • Popups
  • Pipelines
    • Commercial Plus

Navigation Builder

The NavigationBuilder is new to Prism.Maui and is meant to solve a number of issues that developers run into. While Prism's NavigationService will always continue to be URI based, this can create issues for very complex Deep Links and can cause issues for developers who are doing some string interpolation.

public static class NavigationKeys
{
    public const string ViewA = nameof(ViewA);
    public const string ViewB = nameof(ViewB);
}

public static class NavParameterKeys
{
    public const string Id = nameof(Id);
}

public class ViewAViewModel : BindableBase
{
    private async void OnNavigateCommandExecuted()
    {
        await NavigationService.NavigateAsync($"{NavigationKeys.ViewA}/{NavigationKeys.ViewB}");
    }
}

The NavigationBuilder seeks to solve this issue by making the code more readable, easier to maintain, and expose a few new tricks that can help for the first time allow ViewModel based Navigation.

Getting the NavigationBuilder

The NavigationBuilder can NOT be injected into anything with Dependency Injection. It is a one time use API and is created from the INavigationService.

public class ViewAViewModel : BindableBase
{
    private void OnNavigateCommandExecuted()
    {
        var builder = NavigationService.CreateBuilder();
    }
}

Building the NavigationURI

it is important to remember that the order in which you add Navigation Segments to the URI will be the order in which they will be added to the Navigation URI. If I want to navigate Modally to ViewA/ViewB then this would look like:

NavigationService.CreateBuilder()
    .AddSegment(NavigationKeys.ViewA)
    .AddSegment(NavigationKeys.ViewB);

ViewModel First Navigation

As mentioned the NavigationBuilder exposes the ONLY API in Prism that allows ViewModel First Navigation. In order to use ViewModel Navigation you MUST register BOTH the View and the ViewModel.

container.RegisterForNavigation<ViewA, ViewAViewModel>();

If you do not add both the Registration will not exist for the ViewModel to be able to resolve the appropriate navigation key. To use ViewModel Navigation you simply need to supplement the string for the generic ViewModel type.

NavigationService.CreateBuilder()
    .AddSegment<ViewAViewModel>()

Navigation Pages

As you may have previously read, Prism will automatically register the PrismNavigationPage for Navigation. Regardless of whether this is the registered NavigationPage or you have provided your own NavigationPage type, as long as there is a SINGLE registration for a NavigationPage, you can use the helper extension which will check the View Registrations for the proper Navigation Name of the registered NavigationPage.

NavigationService.CreateBuilder()
    .AddNavigationPage()

Tabbed Pages

Similar to the NavigationPage, Prism automatically registers the MAUI TabbedPage for Navigation. You can dynamically create your TabbedPages as follows.

Note

We strongly advise that you DO NOT create a TabbedPage and Manually Add Children.

NavigationService.CreateBuilder()
    .AddTabbedSegment(b =>
        b.CreateTab("ViewA")
         .CreateTab(t => t.AddNavigationPage().AddSegment("ViewB")));

As you may have noticed the TabbedSegmentBuilder has special methods for Creating Tabs and Providing the Selected Tab. The CreateTab method additionally has its own builder that can help you to create Deep Links for individual tabs.

Warning

Deep Linked Tabs are planned for Prism.Maui but are not available in the Beta

Relative or Absolute Navigation

By Default the generated Navigation URI will be a Relative URI. However we do expose methods for explicitly setting the URI to be Relative or Absolute.

NavigationService.CreateBuilder()
    .UseRelativeNavigation();

NavigationService.CreateBuilder()
    .UseAbsoluteNavigation();

In the event that you have some sort of boolean that you need to pass in to control whether it is Relative or Absolute you can pass in as follows:

NavigationService.CreateBuilder()
    .UseAbsoluteNavigation(myCondition);

Adding URI Parameters

By Default the string added will be treated as though you are adding a URI so you may either add them directly or use the Segment Builder as follows:

NavigationService.CreateBuilder
    .AddSegment("ViewA?id=5")
    .AddSegment(NavigationKeys.ViewB, s => s.AddParameter(NavParameterKeys.Id, 6));
Note

When using Segment parameters these will always append to the URI. These will be available in the NavigationParameters passed to the ViewModel for the Specific NavigationSegment they are added to. For instance if the ViewAViewModel implements IInitialize, you would expect the id key to have a value of 5.

Adding Navigation Parameters

Unlike Segment/URI Parameters, NavigationParameters added to the NavigationBuilder are passed to every page being navigated to. Unlike when we add a Segment, we can add parameters in any order we want. You can add them one at a time or as a full list.

var parameters = new NavigationParameters
{
    { "foo", foo },
    { "bar", bar }
};

NavigationService.CreateBuilder
    .AddParameter(NavParameterKeys.Id, 5)
    .WithParameters(parameters);

Navigating

Once you're finished building the Navigation URI, of course there is the most important part... Navigating! The NavigationBuilder has a number of Extension Methods to make it easier for you to navigate from any context and help you avoid async void.

// NOTE: We will not get the NavigationResult back as it was not awaited
NavigationService.CreateBuilder()
    .AddSegment(NavigationKeys.ViewA)
    .Navigate();

For those who may be dropping this into existing code you can of course simply call the classic NavigateAsync and get the INavigationResult back.

var result = await NavigationService.CreateBuilder()
    .AddSegment(NavigationKeys.ViewA)
    .NavigateAsync();

Additionally we provide both Async and non-Async methods which simply allow you to provide a callback OnSuccess or OnError.

builder.Navigate(() => Console.WriteLine("Navigation Successful"));
await builder.NavigateAsync(() => Console.WriteLine("Navigation Successful"));

builder.Navigate(ex => Console.WriteLine($"Navigation Error:\n{ex}"));
await builder.NavigateAsync(ex => Console.WriteLine($"Navigation Error:\n{ex}"));

builder.Navigate(ex => Console.WriteLine(() => Console.WriteLine("Navigation Successful"), $"Navigation Error:\n{ex}"));
await builder.NavigateAsync(() => Console.WriteLine("Navigation Successful"), ex => Console.WriteLine($"Navigation Error:\n{ex}"));
  • Edit on GitHub
  • Ask questions
  • Follow @PrismLib
  • Follow @BrianLagunas
  • Follow @DanJSiegel
Back to top Copyright 2015-2024 Prism Software, LLC