How to Fix NavigationFailed for Singleton Pages in MAUI

Fix the NavigationFailed error in .NET MAUI Shell when navigating to singleton pages by disabling animation in GoToAsync and using a try-catch block.

When using Shell.Current.GoToAsync to navigate to a singleton page, if animations are not disabled, there is a certain probability of encountering a Microsoft.UI.Xaml.Controls.Frame.NavigationFailed was unhandled error.
To avoid this issue, set the animate parameter to false, i.e., await Shell.Current.GoToAsync($"..", false), to reduce the likelihood of errors, and include try-catch handling.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
public static async Task GoToAsync(string uri)
{
    string sourceUri = Shell.Current.CurrentState.Location.OriginalString;
    try
    {
        await Shell.Current.GoToAsync(uri, false);
    }
    catch
    {
        Shell.Current.GoToAsync(sourceUri, false);
        _ = Task.Run(() =>
        {
            MainThread.BeginInvokeOnMainThread(() =>
            {
                Task.Delay(50).Wait();
                Shell.Current.GoToAsync(uri, false);
            }));
        }
    }
}
Built with Hugo
Theme Stack designed by Jimmy