首页 > 解决方案 > Xamarin 表单工具栏项在选项卡式页面中出现两次

问题描述

我在标签页中添加了一个名称刷新的工具栏项,下面是标签页的代码

<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
            xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
            xmlns:d="http://xamarin.com/schemas/2014/forms/design"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:Xapp;assembly=Xapp"
            mc:Ignorable="d"
            x:Class="Xapp.HomePage">
    
    <TabbedPage.ToolbarItems>
        <ToolbarItem x:Name="Refresh" Text="Refresh" />
    </TabbedPage.ToolbarItems>

    <TabbedPage.Children>
        <NavigationPage Title="report">
            <x:Arguments>
                <local:ReportPage>
                    <StackLayout>
                        <Label Text="In:" HorizontalOptions="FillAndExpand" />
                        <Label Text="{Binding CountIn}" FontSize="Medium" FontAttributes="Bold" />
                        <Label Text="Out:" HorizontalOptions="FillAndExpand" />
                        <Label Text="{Binding CountOut}" FontSize="Medium" FontAttributes="Bold" />
                        <Label Text="Date:" HorizontalOptions="FillAndExpand" />
                        <Label Text="{Binding createdon}" FontSize="Medium" FontAttributes="Bold" />
                    </StackLayout>
                </local:ReportPage>
            </x:Arguments>
        </NavigationPage>
 
        <NavigationPage Title="history">
            <x:Arguments>
                <local:HistoryPage />
            </x:Arguments>
        </NavigationPage>
    </TabbedPage.Children>
   
</TabbedPage>

问题是工具栏中出现两次刷新,如下图所示:

错误图片

标签: xamlxamarinxamarin.formsxamarin.androidxamarin.ios

解决方案


如果 App 的MainPageTabbedPage。您无需将其设置为NavigationPage。在应用程序中。

修改 App.Xaml.CS 中的代码

public App()
{
   InitializeComponent();

   MainPage = new HomePage();
}

更新

ContentPage导航到TabbedPage不是一个好的设计 。

所以修改代码为

App.Current.MainPage = new HomePage();

代替page.Navigation.PushAsync(new HomePage());


推荐阅读