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

房东的猫 提交于 2020-06-29 04:19:17

问题


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:


Do you want to achieve it like this GIF?

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

        }


来源:https://stackoverflow.com/questions/60518089/xamarin-form-can-add-tabs-in-shell-app-programmatically-on-the-contentpage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!