| MAUI-TabbedPage | maui-Shell-Lifecycle | |
Shell in .NET MAUI |
In .NET MAUI, the Shell is the modern, high-level container that simplifies app structure and navigation. It’s designed to replace older navigation patterns like NavigationPage, TabbedPage, and FlyoutPage by unifying them into a single, declarative model.
In .NET MAUI, Shell is a powerful navigation container introduced to simplify building complex, multi-page applications. It provides a declarative way to define app structure and navigation using a single hierarchy, reducing boilerplate code compared to traditional navigation patterns like FlyoutPage or TabbedPage.
Shell acts as the application’s navigation framework, offering:
No need to manually manage NavigationPage stacks. Navigate using routes like:
//C#
await Shell.Current.GoToAsync("//home/details");
Optimized for cross-platform navigation.
Shell uses three main elements:
<Shell xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
Title="My App"
FlyoutBehavior="Flyout">
<!-- Flyout Menu -->
<FlyoutItem Title="Home" Icon="home.png">
<ShellContent ContentTemplate="{DataTemplate local:HomePage}" />
</FlyoutItem>
<FlyoutItem Title="Settings" Icon="settings.png">
<ShellContent ContentTemplate="{DataTemplate local:SettingsPage}" />
</FlyoutItem>
</Shell>
Register routes:
//C#
Routing.RegisterRoute("details", typeof(DetailsPage));
Navigate:
//C#
await Shell.Current.GoToAsync("details");
AppShell.xaml for a unified look.Routing.RegisterRoute for custom pages.GoToAsync) instead of manual push/pop stacks.This example demonstrates how to use Shell in a .NET MAUI application to create a simple navigation structure with a Flyout menu and two pages.
Define the Shell structure with two Flyout items: Home and Settings.
<Shell xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:MyApp"
x:Class="MyApp.AppShell"
FlyoutBehavior="Flyout">
<FlyoutItem Title="Home" Icon="home.png">
<ShellContent ContentTemplate="{DataTemplate local:HomePage}" />
</FlyoutItem>
<FlyoutItem Title="Settings" Icon="settings.png">
<ShellContent ContentTemplate="{DataTemplate local:SettingsPage}" />
</FlyoutItem>
</Shell>
//C#
public partial class AppShell : Shell
{
public AppShell()
{
InitializeComponent();
Routing.RegisterRoute("details", typeof(DetailsPage));
}
}
Use Shell navigation to move to the Details page:
//C#
await Shell.Current.GoToAsync("details");
When you run the app, you will see a Flyout menu with two options: Home and Settings. Selecting an option displays the corresponding page. You can also navigate programmatically to the Details page using Shell routing.
xml
<Shell xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:MyApp"
x:Class="MyApp.AppShell">
<!-- Flyout Menu -->
<FlyoutItem Title="Home" Icon="home.png">
<ShellContent ContentTemplate="{DataTemplate local:MainPage}" />
</FlyoutItem>
<!-- Tabbed Navigation -->
<TabBar>
<ShellContent Title="Dashboard"
ContentTemplate="{DataTemplate local:DashboardPage}" />
<ShellContent Title="Settings"
ContentTemplate="{DataTemplate local:SettingsPage}" />
</TabBar>
</Shell>
csharp
await Shell.Current.GoToAsync("//DashboardPage");
| MAUI-TabbedPage | maui-Shell-Lifecycle | |