Search code examples
xamlxamarinxamarin.formsxamarin.androidxamarin.ios

xamarin navigation login management


At the entrance of the tabbed project I made, I want the login page to come first, then I want the project to open. I tried many ways;

Navigation.PushAsync(new Page1());

Problem; tabbar doesn't show

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

Problem; Nothing happens

Codes;

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>

I tried so many ways hope you can help me solve it.


Solution

  • There are two ways to achieve this .


    Include LoginPage into AppShell

    1. Set AppShell as MainPage in App.

    2. Place Two Tabbar in AppShell , and place LoginPage first than HomePage, and set different Route for the two Tabbar.

      <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. Call await Shell.Current.GoToAsync("//Home"); when login in , Call await Shell.Current.GoToAsync("//Login"); when login out .

    Don't Include LoginPage into AppShell

    1. Set LoginPage as MainPage in App at first.
    2. Call MainPage = new AppShell(); When login in , Call MainPage = new LoginPage(); when login out .