0

在我的应用程序中,我使用 Xamarin Shell 创建了一个底部标签栏导航,如下所示:

主页.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>

ChatbotPage.xaml我想完全删除标签栏,所以我Shell.SetTabBarIsVisible(this, false);在其后面的代码中实现并在其中一个标签中添加了一个 TapGesture 以便它带我回到主页面。
每当我运行应用程序并点击 ChatbotPage 中的后退按钮时,我都会收到此异常
Java.Lang.IllegalArgumentException: 'DrawerLayout must be measured with MeasureSpec.EXACTLY.'
,我不知道这意味着什么,并且想知道是否有任何修复或解决方法?

聊天机器人页面.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());
        }
    }
4

1 回答 1

0

使用 await Navigation.PushAsync(new MainPage());不会带你回到MainPage你正在使用的,这行代码将推送到一个新的MainPage.

要在选项卡栏下以编程方式选择不同的选项卡,您可以使用:

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......
}

我在这里上传了一个样本。

于 2019-11-18T07:09:03.980 回答