-1

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.

  • 2
    The question is not clear, could you reformulate? I thin a better approach is to stay inside Shell (keep the same MainPage) whenever it is possible, take a look at https://stackoverflow.com/q/65186262/ – Cfun May 01 '22 at 23:46
  • 1
    this has been discussed many times - https://www.google.com/search?q=xamarin+appshell+login – Jason May 01 '22 at 23:52
  • one simple approach is to set `MainPage = new LoginPage();` on app startup - then after the login has completed, set `MainPage = new AppShell();` – Jason May 01 '22 at 23:53

1 Answers1

1

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 .
ColeX
  • 14,062
  • 5
  • 43
  • 240