| MAUI-Shell | MAUI-ShellContent | |
Shell LifeCycle |
The Shell lifecycle defines how a Xamarin.Forms or .NET MAUI Shell application initializes, loads pages, and handles navigation. Understanding these stages helps in managing resources, navigation, and UI updates effectively.
App.xaml.cs.OnAppearing() – Called when Shell or a page becomes visible.OnDisappearing() – Called when Shell or a page is hidden.OnNavigating() – Triggered before navigation occurs.OnNavigated() – Triggered after navigation completes.
App.xaml.cs
└── Shell
├── Initialize Routes
├── OnAppearing()
└── TabBar / Flyout
├── Tab
│ └── ShellContent → Page
└── Navigation Events
├── OnNavigating()
└── OnNavigated()
In .NET MAUI Shell, the lifecycle primarily revolves around the movement between pages within your app's visual hierarchy. While it respects the standard .NET MAUI app lifecycle, it introduces specific events to handle navigation and page visibility.
Shell manages when pages appear or disappear. You can hook into these by overriding methods in your ContentPage:
When using Shell's URI-based navigation (Shell.Current.GoToAsync), the following sequence typically occurs:
The lifecycle events fire differently depending on how you navigate:
Shell also interacts with the lower-level Handler Lifecycle, which manages the link between cross-platform code and native controls:
I can help you dive deeper into any of these areas:
public partial class AppShell : Shell
{
public AppShell()
{
InitializeComponent();
Routing.RegisterRoute("details", typeof(DetailsPage));
}
protected override void OnAppearing()
{
base.OnAppearing();
Console.WriteLine("Shell Appeared");
}
protected override void OnNavigating(ShellNavigatingEventArgs args)
{
base.OnNavigating(args);
Console.WriteLine($"Navigating to: {args.Target.Location}");
}
protected override void OnNavigated(ShellNavigatedEventArgs args)
{
base.OnNavigated(args);
Console.WriteLine($"Navigation completed: {args.Current.Location}");
}
}
AppShell constructor for clean navigation.OnAppearing() for UI updates, not heavy logic.OnNavigating() to cancel navigation if needed.OnDisappearing() to avoid memory leaks. | MAUI-Shell | MAUI-ShellContent | |