TL;DR: Want to create a seamless navigation experience in your .NET MAUI app? This guide explores two powerful methods: Shell for an intuitive mobile menu or base navigation pages for tabbed and stacked layouts. Dive in and discover code examples to simplify your app development.
A typical application needs a hierarchical navigation experience where the user can navigate through pages forward and backward as required.
The .NET MAUI platform provides two primary forms of page navigation to an app:
In this blog, let’s see how we can integrate page navigation in your .NET MAUI application with code examples.
Shell page navigation is recommended to provide a page navigation experience in a .NET MAUI mobile app.
Shell is a UI control that hosts your pages and provides flyout and tab menus for navigation. Shell page navigation can also be done based on URLs. You can use content templates with it to make the code efficient.
A Shell template is available in any new .NET MAUI project as an AppShell.Xaml file with a single page that is added as the primary page. To use this template:
<Shell x:Class="ShellPageNavigation.AppShell" xmlns="http://schemas.microsoft.com/dotnet/2021/maui" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:ShellPageNavigation" Shell.FlyoutBehavior="Diabled"> <ShellContent Title="Home" ContentTemplate="{DataTemplate local:MainPage}" Route="MainPage" /> <ShellContent Title="Add" ContentTemplate="{DataTemplate local:AddPage}" Route="AddPage" /> <ShellContent Title="Edit" ContentTemplate="{DataTemplate local:EditPage}" Route="EditPage" /> </Shell>
<Shell x:Class="ShellPageNavigation.AppShell" xmlns="http://schemas.microsoft.com/dotnet/2021/maui" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:ShellPageNavigation" Shell.FlyoutBehavior="Flyout"> <ShellContent Title="Home" ContentTemplate="{DataTemplate local:MainPage}" Route="MainPage" /> <ShellContent Title="Add" ContentTemplate="{DataTemplate local:AddPage}" Route="AddPage" /> <ShellContent Title="Edit" ContentTemplate="{DataTemplate local:EditPage}" Route="EditPage" /> </Shell>
Note: Check out the .NET MAUI Shell flyout and Shell tabs documentation for more details.
Base navigation pages are another way to achieve page navigation in your .NET MAUI app. They support pages such as FlyoutPage, TabbedPage, and NavigationPage. We can perform navigation through Push and Pop actions.
Let’s take a quick look at these pages!
The .NET MAUI TabbedPage contains tabs, and each tab will load content in the detail area.
Refer to the following code example.
<TabbedPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:ShellPageNavigation" x:Class="ShellPageNavigation.TabbedPageNavigation"> <local:MainPage Title="Home"/> <local:AddPage Title="Add"/> <local:EditPage Title="Edit"/> </TabbedPage>
The .NET MAUI FlyoutPage contains a detail page with an overlay page called a flyout to present items. The detail page will load the content of the page selected on the flyout.
Refer to the following code example.
<FlyoutPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:ShellPageNavigation" x:Class="ShellPageNavigation.FlyoutPageNavigation"> <FlyoutPage.Flyout> <local:FlyMenuPage x:Name="flyoutMenu"/> </FlyoutPage.Flyout> <FlyoutPage.Detail> <NavigationPage> <x:Arguments> <local:MainPage/> </x:Arguments> </NavigationPage> </FlyoutPage.Detail> </FlyoutPage>
The .NET MAUI NavigationPage is used to stack pages, and we can easily navigate to the required page with the push and pop actions.
The PushAsync method of the NavigationPage will push a page in the navigation stack.
Refer to the following code.
await Navigation.PushAsync(new DetailsPage());
The PopAsync method of the NavigationPage will pop a page in the navigation stack. Also, we can pop the current page by pressing the Back button on our device.
To programmatically pop a page, refer to the following code.
await Navigation.PopAsync();
Check out a complete example of page navigation in the .NET MAUI application on GitHub.
Thanks for reading. I hope you have a basic idea about page navigation in the .NET MAUI application. Syncfusion .NET MAUI controls are also compatible with these navigation pages. Please give them a try and leave your feedback in the comments section below!
Also, you can contact us through our support forum, support portal, or feedback portal. We are always happy to assist you!