简体   繁体   English

Xamarin 表单:TabbedPage 中的 ContentPages

[英]Xamarin Forms: ContentPages in TabbedPage

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.遗憾的是,我不确定如何使用 XAML 语法来做到这一点。 My dummy project looks like the following:我的虚拟项目如下所示:

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

Page 2 exactly the same.第2页完全一样。 The Tabbed Page:标签页:

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

The Pages just won't show up?页面只是不会显示? How can I do this properly?我怎样才能正确地做到这一点?

You are doing it wrong.你做错了。 You must place the pages as the TabbedPage Children.您必须将页面作为 TabbedPage 子项放置。

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

As on date today, it's not necessary to add "Children" property.截至今天,没有必要添加“儿童”属性。 If your Pages are in another directory say "PagesDirectory".如果您的页面在另一个目录中,请说“PagesDirectory”。 You can reference the content page "Page1" as below, it will work absolutely fine:您可以参考下面的内容页面“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">
<views:Page1 Title="Content Page 1"></views:Page1>
<views:Page2 Title="Content Page 2"></views:Page2>

You are looking for the Children property on the TabbedPage您正在 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM