Xamarin Forms: ContentPages in TabbedPage

前端 未结 3 1687
Happy的楠姐
Happy的楠姐 2021-01-05 06:40

I am trying to put some custom Content Pages into a Tabbed Page. Sadly I am not sure, how to do this with the XAML syntax. My dummy project looks like the following:

相关标签:
3条回答
  • 2021-01-05 07:08

    You are looking for the Children property on the TabbedPage

    <TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
                xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                x:Class="MyApp.Pages.Navigation">
    <TabbedPage.Children>
        <ContentPage Title="Home">
        </ContentPage>
        <ContentPage Title="Browse">
        </ContentPage>
    </TabbedPage.Children>
    </TabbedPage>
    
    0 讨论(0)
  • 2021-01-05 07:14

    As on date today, it's not necessary to add "Children" property. If your Pages are in another directory say "PagesDirectory". You can reference the content page "Page1" as below, it will work absolutely fine:

     <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:views="clr-namespace:YourApp.Views.PagesDirectory"
             mc:Ignorable="d"
             x:Class="YourApp.Views.TabbedPage"
            Title="Tabbed Page">
    
    <views:Page1 Title="Content Page 1"></views:Page1>
    <views:Page2 Title="Content Page 2"></views:Page2>
    

    0 讨论(0)
  • 2021-01-05 07:15

    You are doing it wrong. You must place the pages as the TabbedPage Children.

    Here is the solution:

    <?xml version="1.0" encoding="utf-8" ?>
    <TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
                xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                xmlns:mypages="clr-namespace:MyApp.Pages;assembly=MyApp"
                x:Class="MyApp.Pages.Navigation">
      <TabbedPage.Children>
        <mypages:Page1 Title="Home"/>
        <mypages:Page2 Title="Browse"/>
      </TabbedPage.Children>
    </TabbedPage>
    

    In alternative you can do it programmatically:

    public class TabsPage : TabbedPage
    {
        public TabsPage ()
        {
            this.Children.Add (new Page1 () { Title = "Home" });
            this.Children.Add (new Page2 () { Title = "Browse" });
        }
    }
    
    0 讨论(0)
提交回复
热议问题