XAML 如何使标签栏消失时,导航到详细信息页

gudnpqoy  于 12个月前  发布在  其他
关注(0)|答案(2)|浏览(89)

我正在尝试实现以下简单的导航场景:
我有“第1页”,它有一个选项卡栏,它允许导航到定义为全局路线的“DetailsPage”,只显示一个后退按钮导航回“第1页”。这个视频准确地展示了这个场景:https://user-images.githubusercontent.com/74175405/229085788-05809927-904b-4503-8b33-c8a45204e7a2.mp4
我尝试过使用Shell.SetTabarIsVisible方法的不同方法,但没有成功,最后一个我在做了一些研究后尝试的方法似乎最有凝聚力,分别在FirstPage.xaml.cs和DetailsPage.xaml.cs中这样做:

protected override void OnAppearing()
{
    base.OnAppearing();

    Shell.SetTabBarIsVisible(workoutSelectionPage, false); //This is Page 2, in page 1 I set true 
}

AppShell.xaml:

<TabBar>
    <Tab Title="Page 1">
        <ShellContent ContentTemplate="{DataTemplate local:FirstPage}"/>
    </Tab>
</TabBar>

AppShell.xaml.cs:

Routing.RegisterRoute("Page 2", typeof(DetailsPage));

这是一个非常基本的场景,所以我肯定我错过了一些简单的东西在这里的任何输入是非常感谢,谢谢!

brvekthn

brvekthn1#

我应该像把它放在Ctor一样简单:

public TestView(TestViewModel viewModel)
{
    BindingContext = _viewModel = viewModel;

    InitializeComponent();

    Shell.SetTabBarIsVisible(this, false);
}

在xaml页面上设置它的替代方法

<ContentPage
    x:Class="MauiTest.Views.TestView"
    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"
    Shell.NavBarIsVisible="False"
    Shell.TabBarIsVisible="False">
hmae6n7t

hmae6n7t2#

您只需要在DetailPage中设置SetTabBarIsVisible即可。
下面的代码可以用来实现视频中的场景。

  • AppShell.xaml*,不需要为DetailPage设置ShellContent。
<TabBar>
    <Tab Title="MainPage">
        <ShellContent ContentTemplate="{DataTemplate local:MainPage}"/>
    </Tab>
    <Tab Title="First Page">
        <ShellContent ContentTemplate="{DataTemplate local:FirstPage}"/>
    </Tab>
</TabBar>
  • AppShell.cs *,在此注册路由,
Routing.RegisterRoute("mainpage", typeof(MainPage));
Routing.RegisterRoute("firstpage", typeof(FirstPage));
Routing.RegisterRoute("detailpage", typeof(DetailPage));
  • FirstPage.cs *,使用shell导航,
async void Navigate()
{
    ...
    await Shell.Current.GoToAsync("detailpage");
}

DetailPage.cs,在OnAppearing或构造函数中使用SetTabBarIsVisible

public DetailPage()
{
    InitializeComponent();

    Shell.SetTabBarIsVisible(this, false);
}

这就是效果:

希望能帮上忙!

相关问题