【问题标题】:Shell navigation in .NET MAUI to a page with Bottom Tabs.NET MAUI 中的 Shell 导航到带有底部选项卡的页面
【发布时间】:2022-12-26 02:45:09
【问题描述】:

在登录流程中,登录页面通常没有构成应用程序主要流程的底部选项卡。

AppShell.xaml

 <TabBar>
        <ShellContent Title="Home"
             Icon="home.png" 
                      ContentTemplate="{DataTemplate local:HomePage}"/>
        <ShellContent Title="Articles"
                          Icon="articles.png"
                          ContentTemplate="{DataTemplate local:ArticlesPage}" />
    </TabBar>

因此,如果登录成功,我将尝试从登录页面导航到 HomePage,它是 Shell 中 TabBar 的一部分。问题是 Shell 然后导航到 HomePage,就好像它是一个独立的页面,没有 TabBar。 我假设答案在于导航到 TabBar 部分本身,我不知道。

【问题讨论】:

  • 您是否考虑过将登录页面也包含在您的 TabBar 中,并仅使用可见性来控制显示哪些选项卡? (如果只有一个选项卡可见,shell 会自动隐藏选项卡标题。)
  • 我没有想到这一点。让我试试看

标签: maui .net-maui


【解决方案1】:

有两种方法可以实现您的要求。


将 LoginPage 包含到 AppShell 中

  1. App中将AppShell设置为MainPage

  2. 在 AppShell 中放置两个Tabbar,先放置LoginPage,然后放置HomePage,并为两个Tabbar设置不同的Route

    <TabBar Route="Login">
      <ShellContent  ContentTemplate="{DataTemplate local:LoginPage}" />
    </TabBar>
    
    <TabBar Route="Home">
         <ShellContent Title="Home" Icon="home.png" ContentTemplate="{DataTemplate local:HomePage}"/>
         <ShellContent Title="Articles" Icon="articles.png"  ContentTemplate="{DataTemplate local:ArticlesPage}" />
    </TabBar>
    
  3. 登录时拨打await Shell.Current.GoToAsync("//Home");,退出时拨打await Shell.Current.GoToAsync("//Login");

    不要将 LoginPage 包含到 AppShell 中

    1. 首先将LoginPage设置为App中的MainPage
    2. 登录时调用MainPage = new AppShell();,退出时调用MainPage = new LoginPage();

【讨论】:

    【解决方案2】:

    除了 ColeX 的回答之外,它仍然可以在没有 TabBar 控件的情况下完美运行。

     <ShellContent ContentTemplate="{DataTemplate pages:LoginPage}"
                   Route="LoginPage"/>
    
     <TabBar Route="Home">
         <ShellContent Title="Home" Route="HomePage" Icon="home.png" ContentTemplate="{DataTemplate local:HomePage}"/>
         <ShellContent Title="Articles" Route="ArticlesPage" Icon="articles.png"  ContentTemplate="{DataTemplate local:ArticlesPage}"/>
     </TabBar>
    

    LoginPage.xaml.cs 中的登录按钮事件处理程序

    private async void BtnLogin_Clicked(object sender, EventArgs e)
    {
        // ApiHelper is a static class with methods that communicate with an API.
        var response = await ApiHelper.Login(EntEmail.Text, EntPassword.Text);
    
        if (response)
        {
            await Shell.Current.GoToAsync("//HomePage");
        }
        else
        {
            await DisplayAlert("", "Operation failed", "Cancel");
        }
    }
    

    笔记:EntEmail 和EntPassword 是LoginPage.xaml 中两个Entry 控件的名称。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-23
      • 2021-08-06
      • 1970-01-01
      • 1970-01-01
      • 2021-02-02
      • 2016-02-16
      • 1970-01-01
      • 2020-12-02
      相关资源
      最近更新 更多