工具栏项目 xamarin.forms 消失了

toolbar items xamarin.forms disappeared

我想添加工具栏项以保存用户输入。我使用了 contentpage.toolbar 项,如下面的代码所示:

<ContentPage 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"
             mc:Ignorable="d"
             x:Class="firstXamarin.HistoryPage">

    <ContentPage.ToolbarItems>
        <ToolbarItem Text="Add"
                     Order="Primary"
                     Priority="0"
                     Clicked="Add_OnClicked" />
    </ContentPage.ToolbarItems>

    <ContentPage.Content>
        <StackLayout Margin="30,30">
            <Entry x:Name="UsernameEntry" Placeholder="username" VerticalOptions="Center" Height="50" />
        </StackLayout>
    </ContentPage.Content>

    
</ContentPage>

缺少工具栏项和工具栏。我遵循了这些解决方案,但它们不起作用:

  1. Toolbar item not showing in xamarin forms
  2. ToolbarItem are not shown

这是我的页面的 .cs 文件:

 public HistoryPage()
        {
             InitializeComponent();
        }

    private void Add_OnClicked(object sender, EventArgs e)
        {
            Post post = new Post()
            {
                Username = UsernameEntry.Text
            };
            SQLiteConnection conn = new SQLiteConnection(App.DatabaseLocation);
            conn.CreateTable<Post>();
            int rows = conn.Insert(post);
            conn.Close();

            if (rows > 0)
            {
                DisplayAlert("Success", "the data inserted successfully", "ok");
            }
            else
            {
                DisplayAlert("failure", "the data  are not inserted ", "ok");

            }
        }

注意: 我读过我会使用导航页面,但我没有找到 xaml NavigationPage 实现的示例

标签页代码如下:

  <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:firstXamarin;assembly=firstXamarin"
            mc:Ignorable="d"
            x:Class="firstXamarin.MainPage">
    <TabbedPage.ToolbarItems></TabbedPage.ToolbarItems>
    <local:ReportPage Title="Report" />
    <local:HistoryPage Title="History"/>
</TabbedPage>

{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class MainPage : TabbedPage
    {
        public MainPage()
        {
            InitializeComponent();
        }
    }
}

应用页面代码:

  <Application.Resources>
        <Color x:Key="ButtonColor">CornflowerBlue</Color>
        <Color x:Key="EntryColor">Gainsboro</Color>

        <Style TargetType="Button">
            <Setter Property="BackgroundColor" Value="{StaticResource ButtonColor}"/>
            <Setter Property="TextColor" Value="{StaticResource EntryColor}"/>
        </Style>
    </Application.Resources>
</Application>

 public partial class App : Application
    {
        public static string DatabaseLocation = string.Empty;
        public App()
        {
            InitializeComponent();

            MainPage = new NavigationPage(new MainPage());
           
        }

        public App(string dblocation)
        {
            InitializeComponent();

            MainPage = new NavigationPage(new MainPage());
            DatabaseLocation = dblocation;
        }

        protected override void OnStart()
        {
        }

        protected override void OnSleep()
        {
        }

        protected override void OnResume()
        {
        }
    }

如果App的MainPage是一个TabbedPage .

在App.xaml.cs

 MainPage = new MainPage()

在主页面

将子页面设置为 NavigationPage

<TabbedPage.Children>
    <NavigationPage Title="xxx">           
        <x:Arguments>
            <local:HistoryPage />
        </x:Arguments>
    </NavigationPage>

    
    <NavigationPage Title="xxx">           
        <x:Arguments>
            <local:ReportPage />
        </x:Arguments>
    </NavigationPage>
</TabbedPage.Children>