简体   繁体   English

有什么方法可以在Xamarin.Forms的同一页上插入导航栏和标签栏?

[英]Is there any way to insert a Navigation Bar and a Tab Bar in the same page on Xamarin.Forms?

I'm into a new project and have been stuck trying to insert a Navigation Bar and a Tab Bar in the same page on Xamarin Forms, as the title says. 正如标题所说,我正在一个新项目中,一直试图在Xamarin Forms的同一页面上插入导航栏和选项卡栏而陷入困境。 The problem is: I've only been able to insert one or another, never both. 问题是:我只能插入一个或另一个,而不能两者都插入。 Do you guys know any way to solve this, maybe with a custom renderer, or something like this? 你们是否知道解决此问题的任何方法,例如使用自定义渲染器或类似方法?

Either the XAML code starts with XAML代码要么以

<ContentPage ... />

or it starts with 或者以

<TabbedPage ... />

I'd be glad if someone could help! 如果有人可以帮忙,我会很高兴!

Is there any way to insert a Navigation Bar and a Tab Bar in the same page on Xamarin.Forms? 有什么方法可以在Xamarin.Forms的同一页上插入导航栏和标签栏?

Of course you can implement it! 当然可以实现! You can populate the TabbedPage with a collection of child Page objects, such as a collection of ContentPage or NavigationPage instances.Refer to the following code. 您可以使用子Page对象的集合(例如ContentPage或NavigationPage实例的集合)填充TabbedPage。请参考以下代码。

in App.xaml.cs (HomePage is a TabbedPage) 在App.xaml.cs中(主页是TabbedPage)

public partial class App : Application
{
    public App()
    {
        InitializeComponent();

        MainPage =new HomePage(); 
    }

    protected override void OnStart()
    {
        // Handle when your app starts
    }

    protected override void OnSleep()
    {
        // Handle when your app sleeps
    }

    protected override void OnResume()
    {
        // Handle when your app resumes
    }
}

in HomePage.xaml.cs (Mypage and MyPage1 are ContentPages) 在HomePage.xaml.cs中(Mypage和MyPage1是ContentPages)

public partial class HomePage : TabbedPage
{
    public HomePage()
    {
        InitializeComponent();


        var contentPage = new MyPage1();
        contentPage.Title = "Page1";
        contentPage.Icon = "Mine.png";

        var navigationPage = new NavigationPage(new MyPage());
        navigationPage.Icon = "Mine.png";
        navigationPage.Title = "Page2";

        Children.Add(contentPage);
        Children.Add(navigationPage);

    }

  }
}

Or you can implement it with XAML code in HomePage.xaml 或者,您也可以使用HomePage.xaml中的XAML代码来实现它

<?xml version="1.0" encoding="UTF-8"?>
<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:local="clr-namespace:xxx" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="xxx.HomePage">
  <local:MyPage Title="Page1" Icon="xxx.png"/>    
  <NavigationPage Title="Page2" Icon="xxx.png">
    <x:Arguments>
        <local:MyPage1 />
    </x:Arguments>
  </NavigationPage>
</TabbedPage>

Now the TabbedPage is populated with two child Page objects. 现在,TabbedPage填充了两个子Page对象。 The first child is a ContentPage instance, and the second tab is a NavigationPage containing a ContentPage instance. 第一个子项是ContentPage实例,第二个选项卡是包含ContentPage实例的NavigationPage。 Just like the following images. 就像下面的图片一样。

effect1 EFFECT1

effect2 EFFECT2

a TabbedPage is a container for other pages. TabbedPage是其他页面的容器。 Each tab can contain a NavigationPage as it's child 每个选项卡都可以包含一个子导航页

TabbedPage
  Tab 1 - NavigationPage(ContentPage)
  Tab 2 - ContentPage
  Tab 3 - NavigationPage(ContentPage)

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

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