0

In my app, I've used Xamarin Shell to create a bottom tab bar navigation like so:

MainPage.xaml

<!--TabBar Styles and Resources-->
    <Shell.Resources>
        <ResourceDictionary>
            <Style x:Key="FreeStyle" TargetType="Element">
                <Setter Property="Shell.TabBarBackgroundColor" Value="#f7f7f7" />
                <Setter Property="Shell.TabBarForegroundColor" Value="White"/>
                <Setter Property="Shell.TabBarUnselectedColor" Value="#999999"/>
                <Setter Property="Shell.TabBarTitleColor" Value="#61c9f7"/>
            </Style>
        </ResourceDictionary>
    </Shell.Resources>


    <!--BluePill Pages-->
    <TabBar Style="{StaticResource FreeStyle}">
        <!--TeleMED Tab-->
        <Tab Icon="telemedicineIcon.png">
            <ShellContent ContentTemplate="{DataTemplate views:TelemedicineMainPage}"/>
        </Tab>

        <!--FlemingTab-->
        <Tab Icon="chatbotIcon.png">
            <ShellContent ContentTemplate="{DataTemplate views:ChatbotPage}"/>
        </Tab>

        <!--FirstAid Tab-->
        <Tab Icon="firstaidIcon.png">
            <ShellContent ContentTemplate="{DataTemplate views:FirstAidPage}"/>
        </Tab>

        <!--User Profile Tab-->
        <Tab Icon="profileIcon.png">
            <ShellContent ContentTemplate="{DataTemplate views:UserProfilePage}"/>
        </Tab>
    </TabBar>

In the ChatbotPage.xaml I wanted to remove the tab bar completely so I implemented Shell.SetTabBarIsVisible(this, false); in its code behind and added a TapGesture to one of the labels in order for it to take me back to the MainPage.
Whenever I run the app and tap the back button in ChatbotPage I get this exception
Java.Lang.IllegalArgumentException: 'DrawerLayout must be measured with MeasureSpec.EXACTLY.'
I have no idea what it means and wondering if there are any fixes or workarounds for this?

ChatbotPage.xaml

<!--Chatbot Header and Back Button-->
        <Frame BackgroundColor="#f7f7f7" Grid.Row="0" HasShadow="True">
            <StackLayout Orientation="Horizontal">
                <renderers:FontAwesomeIcon Text="{x:Static helpers:IconsFA.BackArrow}" HorizontalOptions="Start">
                    <Label.GestureRecognizers>
                        <TapGestureRecognizer Tapped="BacktoMain_Button"/>
                    </Label.GestureRecognizers>
                </renderers:FontAwesomeIcon>

                <StackLayout Orientation="Horizontal" HorizontalOptions="CenterAndExpand">
                    <Frame BackgroundColor="Gray" 
                           Padding="0" 
                           CornerRadius="100"
                           HasShadow="False"
                           IsClippedToBounds="True"
                           HeightRequest="35"
                           WidthRequest="35">

                        <Image HorizontalOptions="Center"
                               VerticalOptions="Center"/>
                    </Frame>

                    <Label Text="Fleming"
                           TextColor="Black"
                           FontAttributes="Bold"
                           FontSize="18"
                           VerticalOptions="Center"/>
                </StackLayout>
            </StackLayout>
        </Frame>

ChatbotPage.xaml.cs

[XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class ChatbotPage : ContentPage
    {
        public ChatbotPage()
        {
            InitializeComponent();

            Shell.SetTabBarIsVisible(this, false);
        }

        private async void BacktoMain_Button(object sender, EventArgs e)
        {
            await Navigation.PushAsync(new MainPage());
        }
    }
Draxalot2
  • 77
  • 7

1 Answers1

0

Using await Navigation.PushAsync(new MainPage()); won't take you back to the MainPage you are using, this line of code will push to a new MainPage.

To select different tab programmatically under tabbar, you can use:

private void Button_Clicked(object sender, EventArgs e)
{
    TabBar bar = Shell.Current.Items[0] as TabBar;

    //Select the first Tab
    bar.CurrentItem = bar.Items[0];

    //If you want to select the second tab, you can use bar.CurrentItem = bar.Items[1]; the same as the third tab, the fourth tab......
}

I uploaded a sample here.

nevermore
  • 15,432
  • 1
  • 12
  • 30