Shell Navigation In .Net Maui Complete Guide
Understanding the Core Concepts of Shell Navigation in .NET MAUI
Shell Navigation in .NET MAUI
Key Concepts
Shell
- Definition: Shell is a navigation container that provides a common navigation structure across all platforms.
- Features: Supports TabBar, Flyout, and bottom navigation. It can simplify the navigation model and UI layout management.
Navigation Graph
- Definition: Represents the structure of your application's navigation flow.
- Components: Pages, Routes, and Flyout Items (or TabBar Items).
Routes
- Definition: Unique identifiers for each page in the navigation graph.
- Usage: Enable deep linking and programmatic navigation. Routes can be defined using XAML or C#.
Flyout (Menu)
- Definition: A sidebar that can be toggled to display navigation options.
- Usage: Ideal for applications with multiple top-level navigation sections.
TabBar
- Definition: A set of tabs that users can click to switch between different pages within the same level of the app.
- Usage: Useful for creating bottom navigation bars common in mobile applications.
Bottom Navigation
- Definition: A style of navigation where tabs or icons are placed at the bottom of the screen.
- Usage: Enhances user experience by providing easy access to main sections of the app.
How to Implement Shell Navigation
Setting Up Shell in .NET MAUI
- Create a New MAUI Project: Use Visual Studio or the .NET CLI to create a new MAUI project.
- Define Shell Structure: Use XAML to define your壳 structure. You can create a new
Shell
item or modify the defaultAppShell.xaml
.
Defining Routes and Pages
- Create New Pages: Use Visual Studio to create new content pages.
- Register Routes: In
AppShell.xaml.cs
, register your pages with routes.public AppShell() { Routing.RegisterRoute(nameof(Page1), typeof(Page1)); Routing.RegisterRoute(nameof(Page2), typeof(Page2)); }
Navigating Between Pages
- Programmatic Navigation: Use
Shell.Current.GoToAsync(route)
to navigate to a page.private async void OnButtonClicked(object sender, EventArgs e) { await Shell.Current.GoToAsync(nameof(Page1)); }
- Deep Linking: Use routes to enable deep linking. For example, navigating directly to a specific page using a URL.
- Programmatic Navigation: Use
Handling Flyout and TabBar
- Flyout Items: Define items in the flyout using
FlyoutItem
tags.<FlyoutItem Route="Page1" Title="Page 1" /> <FlyoutItem Route="Page2" Title="Page 2" />
- TabBar Items: Define items in the TabBar using
Tab
tags.<Tab> <ShellContent Route="Page1" Title="Page 1" /> <ShellContent Route="Page2" Title="Page 2" /> </Tab>
- Flyout Items: Define items in the flyout using
Customizing Shell Appearance
- Colors and Fonts: Customize the appearance of the flyout, tabs, and navigation bars using styles and templates.
- Icons and Images: Use images and icons for better visual representation of navigation options.
Important Information
- Back Navigation: Shell automatically handles back navigation, but you can customize it by adjusting navigation stacks.
- State Management: Ensure to manage page state properly to provide a seamless user experience.
- Resource Management: Release resources and handle memory efficiently to avoid performance issues.
- Responsive Design: Use responsive layouts to ensure Shell Navigation looks good on all device sizes and orientations.
Online Code run
Step-by-Step Guide: How to Implement Shell Navigation in .NET MAUI
Prerequisites:
- Ensure you have .NET MAUI installed.
- Have Visual Studio 2022 or later, or Visual Studio for Mac with .NET MAUI support.
Step 1: Create a New .NET MAUI Project
- Open Visual Studio.
- Create a new project.
- Select "MAUI App".
- Configure your project details (name, location, etc.) and click "Create".
Step 2: Add NuGet Packages (if needed)
.NET MAUI comes with built-in functionality for XAML shells and navigation, so typically you don't need to install additional NuGet packages.
Step 3: Define Your Shell Structure
In this example, we'll create three simple pages and define a basic shell structure.
Create the First Page (MainPage.xaml
)
Right-click on the Pages
folder (or wherever you want to keep your pages), select Add -> New Item
, and add a new Content Page named MainPage.xaml
.
MainPage.xaml:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="YourAppNamespace.MainPage"
Title="Main Page">
<StackLayout>
<Label Text="Welcome to the Main Page!"
FontSize="Large"
HorizontalOptions="Center"
VerticalOptions="CenterAndExpand"/>
<Button Text="Go to Page 1"
Command="{Binding NavigateToPage1Command}"
HorizontalOptions="Center" />
</StackLayout>
</ContentPage>
Create the Code-behind for the first page (MainPage.xaml.cs
).
MainPage.xaml.cs:
using Microsoft.Maui.Controls;
using System.Windows.Input;
namespace YourAppNamespace.Pages
{
public partial class MainPage : ContentPage
{
public ICommand NavigateToPage1Command { get; }
public MainPage()
{
InitializeComponent();
BindingContext = this;
NavigateToPage1Command = new Command(NavigateToPage1);
}
private async void NavigateToPage1()
{
await Shell.Current.GoToAsync("//page1");
}
}
}
Create the Second Page (Page1.xaml
)
Right-click on the Pages
folder, select Add -> New Item
, and add a new Content Page named Page1.xaml
.
Page1.xaml:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="YourAppNamespace.Pages.Page1"
Title="Page 1">
<StackLayout>
<Label Text="Welcome to Page 1!"
FontSize="Large"
HorizontalOptions="Center"
VerticalOptions="CenterAndExpand"/>
<Button Text="Go back to Main Page"
Command="{Binding GoBackCommand}"
HorizontalOptions="Center" />
<Button Text="Go to Page 2"
Command="{Binding NavigateToPage2Command}"
HorizontalOptions="Center" />
</StackLayout>
</ContentPage>
Create the Code-behind for the second page (Page1.xaml.cs
).
Page1.xaml.cs:
using Microsoft.Maui.Controls;
using System.Windows.Input;
namespace YourAppNamespace.Pages
{
public partial class Page1 : ContentPage
{
public ICommand GoBackCommand { get; }
public ICommand NavigateToPage2Command { get; }
public Page1()
{
InitializeComponent();
BindingContext = this;
GoBackCommand = new Command(() => Shell.Current.GoToAsync(".."));
NavigateToPage2Command = new Command(() => Shell.Current.GoToAsync("//page2"));
}
}
}
Create the Third Page (Page2.xaml
)
Right-click on the Pages
folder, select Add -> New Item
, and add a new Content Page named Page2.xaml
.
Page2.xaml:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="YourAppNamespace.Pages.Page2"
Title="Page 2">
<StackLayout>
<Label Text="Welcome to Page 2!"
FontSize="Large"
HorizontalOptions="Center"
VerticalOptions="CenterAndExpand"/>
<Button Text="Go back to Page 1"
Command="{Binding GoBackCommand}"
HorizontalOptions="Center" />
</StackLayout>
</ContentPage>
Create the Code-behind for the third page (Page2.xaml.cs
).
Page2.xaml.cs:
using Microsoft.Maui.Controls;
using System.Windows.Input;
namespace YourAppNamespace.Pages
{
public partial class Page2 : ContentPage
{
public ICommand GoBackCommand { get; }
public Page2()
{
InitializeComponent();
BindingContext = this;
GoBackCommand = new Command(() => Shell.Current.GoToAsync(".."));
}
}
}
Step 4: Define Shell Routes
Modify the AppShell.xaml
to define routes for your pages.
AppShell.xaml:
<?xml version="1.0" encoding="UTF-8" ?>
<Shell xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="YourAppNamespace.AppShell">
<!-- Define the routes here -->
<ShellContent Title="Home"
Route="home"
ContentTemplate="{DataTemplate local:MainPage}" />
<ShellContent Title="Page 1"
Route="page1"
ContentTemplate="{DataTemplate local:Page1}" />
<ShellContent Title="Page 2"
Route="page2"
ContentTemplate="{DataTemplate local:Page2}" />
</Shell>
Ensure the xmlns:local
attribute includes the right namespace reference in your AppShell.xaml
.
AppShell.xaml (Root):
<?xml version="1.0" encoding="UTF-8" ?>
<Shell xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:YourAppNamespace.Pages"
x:Class="YourAppNamespace.AppShell">
<!-- Define the routes here -->
<TabBar>
<ShellContent Title="Home"
Route="home"
ContentTemplate="{DataTemplate local:MainPage}" />
<ShellContent Title="Page 1"
Route="page1"
ContentTemplate="{DataTemplate local:Page1}" />
<ShellContent Title="Page 2"
Route="page2"
ContentTemplate="{DataTemplate local:Page2}" />
</TabBar>
</Shell>
Step 5: Configure App.xaml.cs
Ensure that the AppShell
is set as the root page of your application.
App.xaml.cs:
using Microsoft.Maui.Controls;
namespace YourAppNamespace
{
public partial class App : Application
{
public App()
{
InitializeComponent();
MainPage = new AppShell();
}
}
}
Step 6: Run the Application
- Press F5 or click the "Start Debugging" button in Visual Studio.
- The application should open displaying the
MainPage
. - Click the "Go to Page 1" button to navigate to
Page1
. - From
Page1
, click the "Go back to Main Page" button to go back toMainPage
. - From
Page1
, click the "Go to Page 2" button to navigate toPage2
. - From
Page2
, click the "Go back to Page 1" button to go back toPage1
.
Summary:
This example illustrated how to:
- Create multiple pages (
MainPage.xaml
,Page1.xaml
,Page2.xaml
). - Define route mappings using
AppShell.xaml
. - Navigate between different pages using commands bound to buttons.
- Use the
Shell.Current.GoToAsync()
method for navigation.
Remember, Shell
navigation in .NET MAUI provides a flexible way to handle navigation in your applications, especially when working with tabs, flyouts, and other complex layouts. The double slash (//
) before a route name tells the shell to reset the navigation stack to this page. The single dot (..
) navigates one page back in the stack.
Login to post a comment.