Advertisement
Advanced .NET MAUI, Shell Navigation

How do you handle navigation lifecycle and prevent back navigation in Shell?

Short answer

Implement IQueryAttributable for parameter handling, and override OnNavigating/OnNavigated or handle Shell.Navigating to inspect or cancel navigation (e.g., unsaved changes).

Save .NET MAUI mock interview
Advertisement
AModel answer
Implement IQueryAttributable for parameter handling, and override OnNavigating/OnNavigated or handle Shell.Navigating to inspect or cancel navigation (e.g., unsaved changes). Set args.Cancel() in the Navigating event, or use OnBackButtonPressed on a page to intercept hardware back.
protected override void OnNavigating(ShellNavigatingEventArgs args)
{
    if (HasUnsavedChanges && args.CanCancel)
        args.Cancel();
    base.OnNavigating(args);
}
Real-world example
An edit screen prompts 'Discard changes?' by cancelling navigation when the user tries to leave with a dirty form.
Likely follow-ups
  • How do you intercept the hardware back button? | Which event fires before navigation?
Related topics