Xamarin form: Can Add tabs in Shell App programmatically on the contentpage

Viewed 1806

I finding through the google but could not find the result.

I have a Shell setup Xamarin form application. Eventhough I am not fully use it's functionally but there is a tab setup in xaml file.

I am wonder can i have a content page hosted under the shellContent and add Tabs from the contentPage.

1 Answers

Do you want to achieve it like this GIF?

enter image description here

You can add x:Name for TabBar in AppShell.xml like this code.

   <TabBar x:Name="myTabBars">
        <Tab Title="Browse" Icon="tab_feed.png">
            <ShellContent ContentTemplate="{DataTemplate local:ItemsPage}" />
        </Tab>
        <Tab Title="About" Icon="tab_about.png">
            <ShellContent ContentTemplate="{DataTemplate local:AboutPage}" />
        </Tab>
    </TabBar>

In the AppShell.xml.cs, expose this tabbar.

   public partial class AppShell : Xamarin.Forms.Shell
    {
        //  public static Shell myshell;
        public static TabBar mytabbar;
        public AppShell()
        {
            InitializeComponent();

             mytabbar = myTabBars;


        }
    }

Use it in the ContentPage.

       private void Button_Clicked(object sender, EventArgs e)
        {


            ShellSection shell_section = new ShellSection
            {
                Title = "home",

            };

            shell_section.Items.Add(new ShellContent() { Content = new HomePage() });
            AppShell.mytabbar.Items.Add(shell_section);

        }
Related