dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
22.21k stars 1.75k forks source link

Maui Shell weird navigation issue with timing of ApplyQueryAttributes and Page Lifecycle #24241

Open meopoc opened 2 months ago

meopoc commented 2 months ago

Description

My app navigates to a MainPage with a menu that then navigates through other pages in the app until it reaches a page that requires a Flyout Menu (at which point I set the Shell.Current.FlyoutContent).

So

MainPage > PageA > PageB > PageWithFlyoutMenu

I'm performing the navigation as follows

MainPage to PageA: await Shell.Current.GoToAsync($"PageA", true);

Lifecycle event order: Constructor > ApplyQueryAttributes > OnAppearing > OnNavigatedTo

PageA to PageB: await Shell.Current.GoToAsync($"PageB");

Lifecycle event order: Constructor > ApplyQueryAttributes > OnAppearing > OnNavigatedTo

PageB to PageWithFlyoutMenu: Shell.Current.FlyoutContent = IPlatformApplication.Current?.Services.GetService<MenuPage>(); await Shell.Current.GoToAsync($"///PageWithFlyoutMenu-1");

Lifecycle event order: Constructor > OnAppearing > OnNavigatedTo > ApplyQueryAttributes

The issue here is that ApplyQueryAttributes is the last to be invoked.

AppShell is as follows:

<Shell
x:Class="****.AppShell"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:****.Views"
xmlns:localDetail="clr-namespace:****.Views.DetailPages"
Title="****">

<ShellContent
    Title="MainPage"
    ContentTemplate="{DataTemplate local:MainPage}"
    FlyoutItemIsVisible="False"
    Route="MainPage"
    Shell.FlyoutItemIsVisible="False" />

<ShellContent
    Title="PageWithFlyoutMenu-1"
    ContentTemplate="{DataTemplate localDetail:PageWithFlyoutMenu-1}"
    Route="PageWithFlyoutMenu-1" />

<ShellContent
    Title="PageWithFlyoutMenu-2"
    ContentTemplate="{DataTemplate localDetail:PageWithFlyoutMenu-2}"
    Route="PageWithFlyoutMenu-2" />

</Shell>

And I have the following routes registered:

Routing.RegisterRoute("PageA", typeof(PageA));
Routing.RegisterRoute("PageB", typeof(PageB));

Can anyone shed any light on why this would be?

Thanks in advance

Steps to Reproduce

No response

Link to public reproduction project repository

No response

Version with bug

8.0.20 SR4

Is this a regression from previous behavior?

Not sure, did not test other versions

Last version that worked well

Unknown/Other

Affected platforms

Android

Affected platform versions

Android 14

Did you find any workaround?

No response

Relevant log output

No response

github-actions[bot] commented 2 months ago

Hi I'm an AI powered bot that finds similar issues based off the issue title.

Please view the issues below to see if they solve your problem, and if the issue describes your problem please consider closing this one and thumbs upping the other issue to help us prioritize it. Thank you!

Open similar issues:

Note: You can give me feedback by thumbs upping or thumbs downing this comment.

meopoc commented 2 months ago

ApplyQueryAttributes.zip

Reproduction project repository attached

Zhanglirong-Winnie commented 2 months ago

This issue has been verified using Visual Studio 17.12.0 Preview 1.0 (8.0.80 & 8.0.72 & 8.0.21). Can repro on Android platform.

PureWeen commented 2 months ago

Yea :-/ the OnNavigatedTo unfortunately don't fire in the most consistent order

I have a PR here building towards this effort https://github.com/dotnet/maui/pull/23420

meopoc commented 2 months ago

@PureWeen HI, Thanks for the reply. Seems odd considering the query properties are likely to be needed in the page lifecycle events. It looks to me that ApplyQueryAttributes is the issue as this fires after the OnAppearing too.

Do you know of any workaround whereby I can use query properties in a lifecycle event?

meopoc commented 2 months ago

On further investigation this only appears to be an issue the first time the "PageWithFlyoutMenu" is navigated to. I've attached another repo with console logging and fixed a couple of navigation issues.

Please note the Shell.Current.FlyoutContent is set just prior to navigating to "PageWithFlyoutMenu"

FYI only tested on Android.

meopoc commented 2 months ago

ApplyQueryAttributes.zip