MAUI Navigation

Explore the powerful navigation capabilities of .NET MAUI, enabling seamless transitions between pages and complex navigation flows.

Table of Contents

Introduction to MAUI Navigation

.NET MAUI (Multi-platform App UI) provides a robust and flexible navigation system that allows you to build sophisticated user experiences across Android, iOS, macOS, and Windows. Understanding how to navigate between pages is fundamental to creating engaging mobile and desktop applications.

MAUI's navigation model is built around the concept of NavigationPage, which provides a customizable navigation bar and manages a history of pages. For more advanced scenarios, .NET MAUI Shell offers a streamlined way to define application structure and navigation.

Passing Data Between Pages

You can pass data to the destination page using navigation parameters. This is particularly useful when navigating to a detail view or a form.

When using Shell.Current.GoToAsync(), you can pass a dictionary of parameters:

// From the source page
var parameters = new Dictionary<string, object>
{
    { "ItemId", 123 },
    { "ItemName", "Sample Item" }
};
await Shell.Current.GoToAsync("detailsPage", parameters);

On the destination page, you can retrieve these parameters by subscribing to the NavigatedTo event or by accessing them directly within the page's constructor or `OnAppearing` method when using Shell's routing.

protected override void OnNavigatedTo(NavigatedToEventArgs args)
{
    base.OnNavigatedTo(args);

    if (args.Parameters.ContainsKey("ItemId"))
    {
        var itemId = (int)args.Parameters["ItemId"];
        // Load item with itemId
    }
}

Handling the Back Button

MAUI provides mechanisms to control the back button behavior.

For NavigationPage, the back button is automatically managed. You can control whether a page can be dismissed by using NavigationPage.HasBackButtonProperty attached property or by overriding NavigationPage.ShouldPopOnBackNavigation.

With MAUI Shell, the back navigation is integrated into the shell's routing system. You can define the navigation hierarchy and Shell will manage the back button's visibility and functionality.

To intercept back navigation requests, you can use the BackButtonPressed event or override the OnBackButtonPressed method in your page:

protected override bool OnBackButtonPressed()
{
    // Your custom back button logic here
    // Return true to consume the back button press
    // Return false to allow default back button behavior
    return base.OnBackButtonPressed();
}