xamarin导航登录管理

xamarin navigation login management

我做的分页项目入口,我想让登录页面先出现,然后我想让项目打开。 我尝试了很多方法;

Navigation.PushAsync(new Page1());

问题;标签栏不显示

Routing.RegisterRoute("Page1", typeof(Page1));

问题;什么都没发生

代码;

AppShell.xaml.cs

public partial class AppShell : Shell
{
    public AppShell()
    {
        InitializeComponent();
        Routing.RegisterRoute(nameof(page1), typeof(page1));
        Routing.RegisterRoute(nameof(page2), typeof(page2));
        Routing.RegisterRoute(nameof(page3), typeof(page3));
    }

}

AppShell.xaml

<TabBar>
    <ShellContent Route="page1" ContentTemplate="{DataTemplate local:page1}" />
    <ShellContent Route="page2" ContentTemplate="{DataTemplate local:page2}" />
    <ShellContent Route="page1" ContentTemplate="{DataTemplate local:page1}" />
</TabBar>

App.xaml.cs

public partial class App : Application
{
    public App()
    {
        InitializeComponent();

        App.Current.MainPage = new NavigationPage(new LoginPage());
        //MainPage = new NavigationPage(new LoginPage());
    }

    protected override void OnStart()
    {
    }

    protected override void OnSleep()
    {
    }

    protected override void OnResume()
    {
    }
}

App.xaml

<Application.Resources>

</Application.Resources>

我试了很多方法希望你能帮我解决。

有两种方法可以实现。


将 LoginPage 包含到 AppShell

  1. AppShell设置为App中的MainPage

  2. 在AppShell中放置两个TabbarLoginPage先于HomePage,两个[=14=设置不同的Route ].

    <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");

不要将 LoginPage 包含到 AppShell 中

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