1

I want to add toolbar item to save the user input. I used the contentpage.toolbar items as shown in the code below:

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

the toolbar item and the toolbar are missed. I followed these solutions but they do not work:

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

this my .cs file for the page:

 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");

            }
        }

Note: I have read that i would use navigation page but I did not find xaml example for NavigationPage implementation

Below is the code of tabbed page :

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

App Page Code :

  <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()
        {
        }
    }
Alloylo
  • 181
  • 2
  • 12

1 Answers1

1

If the MainPage of App is a TabbedPage .

in App.xaml.cs

 MainPage = new MainPage()

in MainPage

Set the children page as 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>

enter image description here

Lucas Zhang
  • 18,630
  • 3
  • 12
  • 22
  • Thanks a lot @Lucas Zhang - MSFT, I used your code then restart the VS 2019 and rewrite the App.xaml.cs line of code: MainPage =new NavigationPage(new MainPage()); – Alloylo Jul 30 '20 at 16:37
  • Don't forget to accept my answer if it helps you :) – Lucas Zhang Jul 31 '20 at 07:18