C# Xamarin表单:选项卡页中的内容页

C# Xamarin表单:选项卡页中的内容页,c#,xaml,mobile,xamarin,xamarin.forms,C#,Xaml,Mobile,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"

我正在尝试将一些自定义内容页放入选项卡式页面。遗憾的是,我不知道如何使用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>

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


这些页面就是不显示?如何正确执行此操作?

您正在选项卡页上查找Children属性

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

你做错了。 必须将页面作为选项卡页子项放置

以下是解决方案:

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

到今天为止,没有必要添加“儿童”属性。如果您的页面位于另一个目录中,请说“PagesDirectory”。您可以参考内容页“Page1”,如下所示,它将非常好地工作:

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



XAML命名空间中的属性类只能在根元素上访问。!另一个答案更详细,但解决方案是相同的,他的错误是没有为孩子们使用正确的标签。我在示例中正确添加的内容!!顺便说一下,另一个答案和我的答案是同时添加的。所以,这和我后来添加我的不是为了说明什么,我不是说你在说明什么,但是OP想把它链接到已经创建的页面,除非它是在代码隐藏中指定的,或者是通过类属性指定的,它是无用的。你的新答案只会创建虚拟内容页,但不会链接到他已经创建的
Page1
Page2
,因此你投了反对票。我也有同样的案子,因为它很管用!不知道为什么要下载,但这是最干净的方式!
 <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>