Xamarin表单:TabbedPage中的ContentPages

kyl*_*ver 6 c# mobile xaml xamarin xamarin.forms

我试图将一些自定义内容页面放入选项卡页面.遗憾的是,我不确定,如何使用XAML语法执行此操作.我的虚拟项目如下所示:

第1页

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
            xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
            x:Class="MyApp.Pages.Page1">
<Label Text="Page 1" VerticalOptions="Center" HorizontalOptions="Center" />
</ContentPage>
Run Code Online (Sandbox Code Playgroud)

第2页完全相同.标签页:

<?xml version="1.0" encoding="utf-8" ?>
<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
            xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
            x:Class="MyApp.Pages.Navigation">
    <ContentPage x:Class="MyApp.Pages.Page1" Title="Home">
    </ContentPage>
    <ContentPage x:Class="MyApp.Pages.Page2" Title="Browse">
    </ContentPage>
</TabbedPage>
Run Code Online (Sandbox Code Playgroud)

页面不会出现?我该怎么做呢?

jze*_*ino 12

你做错了.您必须将页面放置为TabbedPage Children.

这是解决方案:

<?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>
Run Code Online (Sandbox Code Playgroud)

另外,您可以通过编程方式执行此操作:

public class TabsPage : TabbedPage
{
    public TabsPage ()
    {
        this.Children.Add (new Page1 () { Title = "Home" });
        this.Children.Add (new Page2 () { Title = "Browse" });
    }
}
Run Code Online (Sandbox Code Playgroud)