XAML 在MAUI应用程序中使用shell从内容页面导航回选项卡页面

1zmg4dgp  于 12个月前  发布在  Shell
关注(0)|答案(2)|浏览(164)

我是MAUI的新手,使用shell来处理导航,并在这个简单的场景中挣扎:我的主页面是一个选项卡式页面,有一个按钮可以重定向到另一个ContentPage(在包含的代码中称为AnotherPage),并且不是选项卡式的:

Shell.Current.GoToAsync("//AnotherPage");

我的AppShell.xml看起来像这样:

<TabBar>
    <Tab>
        <ShellContent ContentTemplate="{DataTemplate local:MainPage}"/>
    </Tab>
    <Tab>
        <ShellContent ContentTemplate="{DataTemplate local:OtherTabbedPage}"/>
    </Tab>
</TabBar>
<ShellContent ContentTemplate="{DataTemplate local:AnotherPage}" Route="AnotherPage" />

该页面有一个退出按钮,其预期行为是重定向到主页,就像应用程序启动时发生的情况一样。为此,它有一个绑定到以下代码的命令:

Shell.Current.GoToAsync("MainPage");

但当执行时,我得到以下异常:
全局路由当前不能是堆栈上的唯一页,因此不支持全局路由的绝对路由。
我的路由在AppShell.xaml.cs文件中注册如下:

Routing.RegisterRoute("MainPage", typeof(MainPage));
    Routing.RegisterRoute("OtherPage", typeof(OtherPage));
    Routing.RegisterRoute("AnotherPage", typeof(AnotherPage));

我做错了什么?
谢谢你的意见

oxf4rvwz

oxf4rvwz1#

您可以在ShellContent对象中注册路由

<TabBar Route="MainTabBar">
    <Tab Title="page1" >
        <ShellContent ContentTemplate="{DataTemplate local:MainPage}" Route="MainPage"/>
    </Tab>
    <Tab Title="page2"> 
        <ShellContent ContentTemplate="{DataTemplate local:OtherTabbedPage}" Route="OtherTabbedPage"/>
    </Tab>
</TabBar>
<ShellContent ContentTemplate="{DataTemplate local:AnotherPage}" Route="AnotherPage" />

而且不需要再次在代码隐藏中注册它们。
然后执行导航:

Shell.Current.GoToAsync("//AnotherPage");

Shell.Current.GoToAsync("//MainTabBar");

有关详细信息,请参阅注册路线和执行导航
希望能帮上忙!

vfh0ocws

vfh0ocws2#

对于任何遇到类似问题的人,正如Liqun Shen-MSFT建议的那样,首先我清理了我的路由定义,并在AppShell.xaml中只定义了一次:

<TabBar Route="MainTabBar">
    <Tab>
        <ShellContent ContentTemplate="{DataTemplate local:MainPage}"/>
    </Tab>
    <Tab>
        <ShellContent ContentTemplate="{DataTemplate local:OtherPage}"/>
    </Tab>
</TabBar>
<ShellContent ContentTemplate="{DataTemplate local:AnotherPage}" Route="AnotherPage" />

我在这里缺少的是**Route=“MainTabBar”**信息,它不允许我从AnotherPage导航到MainPage,当应用程序启动时,底部的标签栏和没有返回按钮,就像这样

Shell.Current.GoToAsync("//MainTabBar");

相关问题