Shell | Navigation
All navigation in Shiny Shell goes through the INavigator interface. Inject it into your ViewModels via constructor injection.
public class MyViewModel(INavigator navigator){ // navigator is ready to use}NavigateTo (Route)
Section titled “NavigateTo (Route)”Navigate to a registered route by name, optionally passing arguments as key-value tuples.
// Simple route navigationawait navigator.NavigateTo("details");
// With argumentsawait navigator.NavigateTo("details", ("Id", 42), ("Name", "Allan"));Arguments are received on the target ViewModel via IQueryAttributable.ApplyQueryAttributes.
NavigateTo (ViewModel)
Section titled “NavigateTo (ViewModel)”Navigate by ViewModel type with an optional configuration action for strongly-typed argument passing.
// Navigate to a ViewModelawait navigator.NavigateTo<DetailViewModel>();
// With strongly-typed property setupawait navigator.NavigateTo<DetailViewModel>(vm => vm.Id = 42);
// With additional route argumentsawait navigator.NavigateTo<DetailViewModel>( vm => vm.Id = 42, ("ExtraArg", "value"));SetRoot
Section titled “SetRoot”Replace the navigation stack with a new root ViewModel.
// Set a new rootawait navigator.SetRoot<HomeViewModel>();
// With configurationawait navigator.SetRoot<HomeViewModel>(vm => vm.WelcomeMessage = "Hello!");GoBack
Section titled “GoBack”Navigate back one or more pages, optionally passing arguments to the previous ViewModel.
// Simple go backawait navigator.GoBack();
// Go back with argumentsawait navigator.GoBack(("Result", "saved"), ("Timestamp", DateTime.UtcNow));
// Go back multiple pagesawait navigator.GoBack(2);
// Go back multiple pages with argumentsawait navigator.GoBack(2, ("Result", "saved"));PopToRoot
Section titled “PopToRoot”Pop the entire navigation stack back to the root page.
await navigator.PopToRoot();
// With argumentsawait navigator.PopToRoot(("Status", "complete"));Display a simple alert dialog.
await navigator.Alert("Error", "Something went wrong", "OK");| Parameter | Type | Description |
|---|---|---|
title | string | Alert title |
message | string | Alert body text |
acceptText | string | Button text (default: “OK”) |
Confirm
Section titled “Confirm”Display a confirmation dialog and return the user’s choice.
bool confirmed = await navigator.Confirm( "Delete Item", "Are you sure you want to delete this?", "Delete", "Cancel");
if (confirmed){ // proceed with deletion}| Parameter | Type | Description |
|---|---|---|
title | string | Dialog title |
message | string | Dialog body text |
acceptText | string | Accept button text (default: “OK”) |
cancelText | string | Cancel button text (default: “Cancel”) |
Navigation Events
Section titled “Navigation Events”INavigator exposes two events for observing navigation lifecycle:
Navigating (Pre-Navigation)
Section titled “Navigating (Pre-Navigation)”Fires before navigation occurs. Provides the source ViewModel instance and destination route.
navigator.Navigating += (sender, args) =>{ // args.FromUri — current location URI // args.FromViewModel — source ViewModel instance (object?) // args.ToUri — destination route URI // args.NavigationType — Push, SetRoot, GoBack, or PopToRoot // args.Parameters — navigation parameters};| Property | Type | Description |
|---|---|---|
FromUri | string? | The current Shell location URI |
FromViewModel | object? | The source page’s ViewModel instance |
ToUri | string | The destination route URI |
NavigationType | NavigationType | Push, SetRoot, GoBack, or PopToRoot |
Parameters | IReadOnlyDictionary<string, object> | Navigation parameters |
Navigated (Post-Navigation)
Section titled “Navigated (Post-Navigation)”Fires after navigation completes and the destination page’s ViewModel is resolved.
navigator.Navigated += (sender, args) =>{ // args.ToUri — destination route URI // args.ToViewModel — destination ViewModel instance (object?) // args.NavigationType — Push, SetRoot, GoBack, or PopToRoot // args.Parameters — navigation parameters};| Property | Type | Description |
|---|---|---|
ToUri | string | The destination route URI |
ToViewModel | object? | The destination page’s ViewModel instance |
NavigationType | NavigationType | Push, SetRoot, GoBack, or PopToRoot |
Parameters | IReadOnlyDictionary<string, object> | Navigation parameters |
Example: Navigation Logger
Section titled “Example: Navigation Logger”Use IMauiInitializeService to hook events for cross-cutting concerns like logging or analytics:
public class NavigationLogger( ILogger<NavigationLogger> logger, INavigator navigator) : IMauiInitializeService{ public void Initialize(IServiceProvider services) { navigator.Navigating += (_, args) => { logger.LogInformation( "Navigating from '{FromUri}' to '{ToUri}' | Type: {NavigationType} | FromViewModel: {FromViewModel}", args.FromUri, args.ToUri, args.NavigationType, args.FromViewModel?.GetType().Name ); };
navigator.Navigated += (_, args) => { logger.LogInformation( "Navigated to '{ToUri}' | Type: {NavigationType} | ToViewModel: {ToViewModel}", args.ToUri, args.NavigationType, args.ToViewModel?.GetType().Name ); }; }}
// Register in MauiProgram.csbuilder.Services.AddSingleton<IMauiInitializeService, NavigationLogger>();Passing Arguments
Section titled “Passing Arguments”Arguments passed during navigation are delivered via the MAUI built-in IQueryAttributable interface.
public class DetailViewModel : IQueryAttributable{ public int Id { get; set; }
public void ApplyQueryAttributes(IDictionary<string, object> query) { if (query.TryGetValue("Id", out var id)) Id = (int)id; }}This works for arguments passed via NavigateTo, GoBack, and PopToRoot.