maui icon indicating copy to clipboard operation
maui copied to clipboard

Pending Navigations still processing exception when navigating to the page

Open mciprijanovic opened this issue 11 months ago • 8 comments

Description

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

Steps to Reproduce

  1. Create simple MAUI app.
  2. In the AppShell put at least 2 pages: 1st loading page, 2nd Login page
  3. Override OnNavigatedTo() method in the LoadingPage. in that method navigate to LoginPage by using await Shell.Current.GoToAsync($"//{Routes.LoginPage}");
  4. Run the app as windows app. Code will crash with the exception "Pending Navigations still processing"
  5. Run the app in Android emuator. All works
  6. Add await Task.Delay(1) before navigating, code will work on Windows too.

Link to public reproduction project repository

No response

Version with bug

7.0.86

Is this a regression from previous behavior?

Not sure, did not test other versions

Last version that worked well

Unknown/Other

Affected platforms

Windows

Affected platform versions

Windows 10.0.19041.0

Did you find any workaround?

If I add await Task.Delay(1) it will work.

Relevant log output

No response

mciprijanovic avatar Sep 24 '23 10:09 mciprijanovic

Hi @mciprijanovic. We have added the "s/needs-repro" label to this issue, which indicates that we require steps and sample code to reproduce the issue before we can take further action. Please try to create a minimal sample project/solution or code samples which reproduce the issue, ideally as a GitHub repo that we can clone. See more details about creating repros here: https://github.com/dotnet/maui/blob/main/.github/repro.md

This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.

ghost avatar Sep 25 '23 14:09 ghost

Hi, very simple sample is created and pushed to: https://github.com/mciprijanovic/NavigationIssue

I upgraded VS2022 and MAUI to the latest version. Sample shows default project created from VS2022 MAUI App template with 2 simple pages. Main page should be loaded by default. In OnNavigated() method of MainPage navigation to LoginPage is done. Crashes on Windows, works on Android emulator. If line Task.Delay(1) is uncommented, works with Windows also.

mciprijanovic avatar Sep 27 '23 11:09 mciprijanovic

@mciprijanovic have you tried this:

I had the same issue only on windows, I felt the Task.Delay(1) workaround a bit cheap and dirty so came up with this code:

if (DeviceInfo.Platform == DevicePlatform.WinUI)
{
    AppShell.Current.Dispatcher.Dispatch(async () =>
    {
        await Shell.Current.GoToAsync($"//{nameof(DashboardPage)}");
    });
}
else
{
    await Shell.Current.GoToAsync($"//{nameof(DashboardPage)}");
}

Maybe you can adapt this to yours and give it a try.

parko65 avatar Nov 30 '23 16:11 parko65

We've added this issue to our backlog, and we will work to address it as time and resources allow. If you have any additional information or questions about this issue, please leave a comment. For additional info about issue management, please read our Triage Process.

ghost avatar Nov 30 '23 19:11 ghost

Maybe to late to respond to this but you could call a command once the page is loaded. This works on Windows and on android emulator as well. Here is a sample code using CommunityToolkit.Maui to register a command for an event :

Register the community toolkit:

        builder
            .UseMauiApp<App>()
            .UseMauiCommunityToolkit()
            .ConfigureFonts(fonts =>
            {
                fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
                fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
            });

On XAML file:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
             xmlns:viewmodels="clr-namespace:SwiftManager.ViewModels"
             x:Class="SwiftManager.Views.LoadingPage"
             x:DataType="viewmodels:LoadingViewModel"
             Title="{Binding Title}">
    <StackLayout HorizontalOptions="Center" VerticalOptions="Center">
        <ActivityIndicator IsRunning="{Binding IsBusy}" HeightRequest="40" WidthRequest="40" HorizontalOptions="Center" VerticalOptions="Center"/>
    </StackLayout>

    <ContentPage.Behaviors>
        <toolkit:EventToCommandBehavior Command="{Binding PerformNavigationCommand}" EventName="Loaded"/>
    </ContentPage.Behaviors>
</ContentPage>

And on ViewModel:

        [RelayCommand]
        private async Task PerformNavigation()
        {
            try
            {
                IsBusy = true;

                if (!IsAuthenticated)
                {
                    await Shell.Current.GoToAsync($"//{nameof(LoginPage)}");
                }

                await Shell.Current.GoToAsync($"//{nameof(DashboardPage)}");
            }
            finally
            {
                IsBusy = false;
            }
        }

Moonprince1 avatar Jan 05 '24 20:01 Moonprince1

Verified this on VS 17.10.0 Preview 2.0(8.0.7). Repro on Windows 11, not repro on Android 14.0-API34 with below Project: MAUINavigationIssue.zip

XamlTest avatar Mar 15 '24 09:03 XamlTest

Also running into this issue.

RuddyOne avatar Apr 09 '24 09:04 RuddyOne

Happens in VS2022 17.9.6 when project is run in Windows, while Android emulator working fine. Happens in the MainPage's NavigatedTo event handler when I'm directing user to LoginPage if user is not authenticated.

Shujee avatar May 16 '24 00:05 Shujee

I am getting a similar exception on windows. When navigating back to a specific page, we clear navigation stack. While clearing the navigation stack, first remove works fine, we get same exception on second removal. Here is the sample code: if (pageToNavigate != null) { var index = pages.IndexOf(pageToNavigate); while (count > index) { _navigation.RemovePage(pages[count]); count--; } }

I get this exception on second iteration: System.InvalidOperationException HResult=0x80131509 Message=Pending Navigations still processing Source=Microsoft.Maui.Controls StackTrace: at Microsoft.Maui.Controls.ShellSection.Microsoft.Maui.IStackNavigation.RequestNavigation(NavigationRequest eventArgs) at Microsoft.Maui.Controls.Handlers.ShellSectionHandler.SyncNavigationStack(Boolean animated, NavigationRequestedEventArgs e) at Microsoft.Maui.Controls.Handlers.ShellSectionHandler.OnNavigationRequested(Object sender, NavigationRequestedEventArgs e) at Microsoft.Maui.Controls.ShellSection.InvokeNavigationRequest(NavigationRequestedEventArgs args) at Microsoft.Maui.Controls.ShellSection.OnRemovePage(Page page) at Microsoft.Maui.Controls.ShellSection.NavigationImpl.OnRemovePage(Page page) at Microsoft.Maui.Controls.Internals.NavigationProxy.RemovePage(Page page) at Microsoft.Maui.Controls.Internals.NavigationProxy.OnRemovePage(Page page) at Microsoft.Maui.Controls.Internals.NavigationProxy.RemovePage(Page page)

For android and iOS it works fine. Its the only issue on windows

vinodgangwar avatar Jun 21 '24 13:06 vinodgangwar