0

So the below is my xamrain forms shell I am having an issue when I am using my tab bar. I have a login page so in my App.xaml.cs I have the folowing

    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());
    }

As you see I am putting putting my main page to the login page however when I click my home button below when I am in my add pages the home page does not navigate back to the main screen.

<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://learn.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>

So for example here I have an issue

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

When I try to come back from this page clicking the home nothing nappens can someone please explain to me as to why that is?

c-sharp-and-swiftui-devni
  • 3,743
  • 4
  • 39
  • 100
  • If using Shell, supposed to be `MainPage = new AppShell();` in App.xaml.cs. And also check AppShell.xaml.cs to see if the route of StudentsPage was registered there. – Shaw Oct 20 '21 at 22:35
  • https://stackoverflow.com/q/65186262/5228202 – Cfun Oct 21 '21 at 15:42

1 Answers1

3

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