Miloš
Miloš

Reputation: 199

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

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 has 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 but works on Android emulator:

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}");        

    }

}

I expected that this simple code will work. I tried to add artificial delay and that succeded but I don't see that as a clean solution. I also searched the web for help, found similar cases but not this one.

Upvotes: 2

Views: 534

Answers (1)

Guangyu Bai - MSFT
Guangyu Bai - MSFT

Reputation: 4586

I tested the code you provided and I found no issue on my side by using the following code.

Here is the code in Appshell.xml:

<ShellContent
        Title="Home"
        ContentTemplate="{DataTemplate local:MainPage}"
        Route="MainPage" />
   
<ShellContent

    Shell.FlyoutBehavior="Disabled"

    FlyoutItemIsVisible="False"        

    Title="Loading"

    ContentTemplate="{DataTemplate local:LoadingPage}"

    Route="LoadingPage" />

<ShellContent

    Shell.FlyoutBehavior="Disabled"

    FlyoutItemIsVisible="False"

    Title="Login"

    ContentTemplate="{DataTemplate local:LoginPage}"

    Route="LoginPage" />

Here is the code in LoadingPage.xaml.cs:

protected async override void OnNavigatedTo(NavigatedToEventArgs args)

    {

        base.OnNavigatedTo(args);

        await Shell.Current.GoToAsync($"//LoginPage");

    }

Upvotes: 0

Related Questions