首页 > 解决方案 > Xamarin 表单外壳与登录屏幕不导航到主页内容混淆

问题描述

所以下面是我的 xamrain forms shell,我在使用标签栏时遇到了问题。我有一个登录页面,所以在我的 App.xaml.cs 中有以下内容

    public App()
    {        
         Syncfusion.Licensing.SyncfusionLicenseProvider
         .RegisterLicense("");//remove license for security
        DevExpress.XamarinForms.Editors.Initializer.Init();
        DevExpress.XamarinForms.DataGrid.Initializer.Init();

        InitializeComponent();

       MainPage = new NavigationPage(new LoginPage());
    }

如您所见,我将主页放入登录页面,但是当我在添加页面中单击下面的主页按钮时,主页不会导航回主屏幕。

<Shell xmlns="http://xamarin.com/schemas/2014/forms" 
   xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
   xmlns:local="clr-namespace:THEHOCKEYLAB.Views"
   Title="THEHOCKEYLAB"
   x:Class="THEHOCKEYLAB.AppShell">

<!--
    The overall app visual hierarchy is defined here, along with navigation.

    https://docs.microsoft.com/xamarin/xamarin-forms/app-fundamentals/shell/
-->

<Shell.Resources>
    <ResourceDictionary>
        <Style x:Key="BaseStyle" TargetType="Element">
            <Setter Property="Shell.BackgroundColor" Value="{StaticResource Primary}" />
            <Setter Property="Shell.ForegroundColor" Value="White" />
            <Setter Property="Shell.TitleColor" Value="White" />
            <Setter Property="Shell.DisabledColor" Value="#B4FFFFFF" />
            <Setter Property="Shell.UnselectedColor" Value="#95FFFFFF" />
            <Setter Property="Shell.TabBarBackgroundColor" Value="{StaticResource Primary}" />
            <Setter Property="Shell.TabBarForegroundColor" Value="White"/>
            <Setter Property="Shell.TabBarUnselectedColor" Value="#95FFFFFF"/>
            <Setter Property="Shell.TabBarTitleColor" Value="White"/>
        </Style>
        <Style TargetType="TabBar" BasedOn="{StaticResource BaseStyle}" />
        <Style TargetType="FlyoutItem" BasedOn="{StaticResource BaseStyle}" />
    </ResourceDictionary>
</Shell.Resources>

<TabBar>
    <ShellContent Title="Menu" Icon="home.png"  ContentTemplate="{DataTemplate local:AdminMenuPage}" />
    <ShellContent Title="Settings" Icon="settings.png" ContentTemplate="{DataTemplate local:SettingsPage}" />
</TabBar>

<!--
    If you would like to navigate to this content you can do so by calling
    await Shell.Current.GoToAsync("//LoginPage");
-->
<TabBar>
    <ShellContent Route="LoginPage" ContentTemplate="{DataTemplate local:LoginPage}" />
</TabBar>

所以例如在这里我有一个问题

private async void btnAddStudent_Clicked(object sender, EventArgs e)
{
  await Shell.Current.GoToAsync("StudentsPage");
}

当我尝试从该页面返回时,单击主页没有任何问题,有人可以向我解释为什么会这样吗?

标签: c#xamarin.forms

解决方案


有两种方法可以实现这一点。


将 LoginPage 包含到 AppShell 中

  1. 设置AppShell为. MainPage_App

  2. Tabbar在 AppShell 中放置两个,并放置在LoginPage第一比,并为两者HomePage设置不同。RouteTabbar

    <TabBar Route="Login">
      <ShellContent  ContentTemplate="{DataTemplate local:LoginPage}" />
    </TabBar>
    
    <TabBar Route="Home">
        <ShellContent Title="Menu" Icon="home.png"  ContentTemplate="{DataTemplate local:AdminMenuPage}" />
        <ShellContent Title="Settings" Icon="settings.png" ContentTemplate="{DataTemplate local:SettingsPage}" />
    </TabBar>
    
  3. await Shell.Current.GoToAsync("//Home");登录时调用,await Shell.Current.GoToAsync("//Login");注销时调用。

不要在 AppShell 中包含 LoginPage

  1. 设置LoginPage为最初MainPageApp
  2. MainPage = new AppShell();登录时调用,MainPage = new LoginPage();注销时调用。

推荐阅读