Maui Shell 奇怪的导航问题与 ApplyQueryAttributes 和页面生命周期的时间相关

问题描述 投票:0回答:1

相对于其他页面生命周期事件,Shell 导航和

ApplyQueryAttributes
的时间安排有点奇怪。

我的应用程序导航到带有菜单的MainPage,然后导航到应用程序中的其他页面,直到到达需要Flyout Menu的页面(此时我设置了

Shell.Current.FlyoutContent
)。

那么

主页 > PageA > PageB > PageWithFlyoutMenu

我正在执行导航如下

主页到A页

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

生命周期事件顺序:

Constructor
>
ApplyQueryAttributes
>
OnAppearing
>
OnNavigatedTo

A页到B页

await Shell.Current.GoToAsync($"PageB");

生命周期事件顺序:

Constructor
>
ApplyQueryAttributes
>
OnAppearing
>
OnNavigatedTo

PageB 到 PageWithFlyoutMenu

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

生命周期事件顺序:

Constructor
>
OnAppearing
>
OnNavigatedTo
>
ApplyQueryAttributes

这里的问题是

ApplyQueryAttributes
是最后一个被调用的。

AppShell如下:

<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>

我注册了以下路线:

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

编辑:按照评论中的要求在PageB中应用QueryAttributes:

public override void ApplyQueryAttributes(IDictionary<string, object> query)
{
    Tracker.WriteLine();

    if (query.ContainsKey("MyDto"))
    {
        var myParameter = query["MyDto"] as MyDto;

        if (myParameter != null) MyId = new Guid(myParameter.MyId);
    }
    
    base.ApplyQueryAttributes(query);
}

任何人都可以解释为什么会这样吗?

回购链接

maui .net-maui.shell
1个回答
0
投票

我根据你的样本重现了这个问题。

经过我的测试,

Shell.Current.FlyoutContentIPlatformApplication.Current?.Services.GetService();

影响Lifecycle事件的调用顺序。当我注释掉这段代码时,首先调用

ApplyQueryAttributes
事件。

我知道这段代码是用来设置弹出窗口的内容的,但是去掉这段代码后,

PageWithFlyoutMenu_1
仍然可以通过侧边栏跳转到
PageWithFlyoutMenu_2
。所以这可以算是解决这个问题的一个变通办法。

© www.soinside.com 2019 - 2024. All rights reserved.