Pending Navigations still processing when navigating from page constructor or OnNavigated method in MAUI

Milos Ciprijanovic 10 Reputation points
2023-09-24T09:50:19.78+00:00

I am using MAUI with .NET 7 and I want to implement login flow. Basic case I have is to have only 2 pages: Dashboard and Login. Third page is Loading page that will have no layout, just serves as a routing page. Flow is:

  • Loading page is the first one in the AppShell definition so it is loaded first
  • In the constructor of the Loading page or in the OnNavigated() overriden method (behaves the same in both cases) I reroute to Login or dashboard page depending if the user has token saved. In this simple code Exception is thrown with the description "Pending Navigations still processing"
  • Code works on Android emulator but not in the Windows app. Windows app starts to work when Task.Delay(1) is set before caling navigation

This is the shell markup:

 <ShellContent
        Shell.FlyoutBehavior="Disabled"
        FlyoutItemIsVisible="False"        
        Title="Loading"
        ContentTemplate="{DataTemplate layout:LoadingPage}"
        Route="{x:Static clientApp:Routes.LoadingPage}" />

    <ShellContent
        Shell.FlyoutBehavior="Disabled"
        FlyoutItemIsVisible="False"
        Title="Login"
        ContentTemplate="{DataTemplate account:LoginPage}"
        Route="{x:Static clientApp:Routes.LoginPage}" />

    <FlyoutItem FlyoutDisplayOptions="AsMultipleItems">
        <ShellContent
        Title="Dashboard"
        ContentTemplate="{DataTemplate dashboard:DashboardPage}"
        Route="{x:Static clientApp:Routes.DashboardPage}" />
    </FlyoutItem>


This is the simple code that doesn't work on windows app:

public partial class LoadingPage : ContentPage
{
	public LoadingPage()
	{
		InitializeComponent();        
		BindingContext = viewModel;
    }

    protected async override void OnNavigatedTo(NavigatedToEventArgs args)
    {
        base.OnNavigatedTo(args);

        //await Task.Delay(1); Will work if I uncomment this
        await Shell.Current.GoToAsync($"//{Routes.LoginPage}");        
    }
}
.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,427 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.