使用选项卡导航打开新页面

时间:2016-10-30 20:21:57

标签: c# xaml xamarin navigation tabbedpage

Iam使用标签导航并使用页面并想要打开一个新页面。

在我的app.xaml.cs中,我创建了导航页面:

public App()
{
    InitializeComponent();
    MainPage = new NavigationPage(new RootPage());
}

在RootPage中填写导航:

public RootPage()
{
        NavigationPage.SetHasNavigationBar(this, false);

        Children.Add(new TestPage1
        {
            Title = "TestPage1"
        });

        Children.Add(new TestPage2
        {
            Title = "TestPage2"
        });
        Children.Add(new TestPage3
        {
            Title = "TestPage3"
        });
}

这种导航效果非常好,看起来不错。 现在我想在TestPage3中打开一个新页面: TestPage3.xaml:

<?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="DemoApplication.TestPage3">
  <Button x:Name="openSetup"  Clicked="ItemClicked" Text="open Settings"/>
</ContentPage>

TestPage3.xaml.cs:

namespace DemoApplication
{
    public partial class TestPage3 : ContentPage
    {
        public TestPage3()
        {
            InitializeComponent();
        }
        void ItemClicked(object sender, EventArgs e)
        {
            Navigation.PushAsync(new TestPage4());
        }
    }
}

这也有效,但看起来并不好。

新内容加载到原始标签导航下方,加载后标签导航消失 - 因此Page4的内容有点跳跃:)

在其他应用程序中,例如soundcloud,它们会做同样的事情,但看起来更顺畅。

有没有什么方法可以让后退导航之间的标签导航更加顺畅?

感谢您的帮助:)

3 个答案:

答案 0 :(得分:4)

如果您想在标签中导航,每个标签都应该拥有自己的NavigationPage。 TabbedPage本身不应该在NavigationPage中。

MainPage = new RootPage();

Children.Add(new NavigationPage(new TestPage3
        {
            Title = "TestPage3"
        }));

答案 1 :(得分:1)

如Jason所说:如果要在维护选项卡的同时在一个选项卡中导航,则每个选项卡都应具有自己的NavigationPage。 TabbedPage本身不应位于NavigationPage中。

这是在XAML中完成的方式:

App.xaml.cs

public App()
{
    InitializeComponent();
    MainPage = new RootPage();
}

RootPage.xaml

<?xml version="1.0" encoding="UTF-8"?>
<TabbedPage xmlns:Pages="clr-namespace:AppName"
            xmlns="http://xamarin.com/schemas/2014/forms"
            xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
            x:Class="AppName.Rootpage">
    <NavigationPage Title="TestPage1">
        <x:Arguments>
            <Pages:TestPage1/>
        </x:Arguments>
    </NavigationPage>
</TabbedPage>

TestPage1.xaml

<?xml version="1.0" encoding="UTF-8"?>
<ContentPage NavigationPage.HasNavigationBar="false"
             xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="AppName.TestPage1">
    <Button Clicked="ButtonClicked"/>
</ContentPage>

TestPage1.xaml.cs

public TestPage1()
{
    InitializeComponent();
}

void ButtonClicked(object sender, EventArgs e)
{
    Navigation.PushAsync(new TestPage2());
}

请注意,通过这种设置,您必须将NavigationPage.HasNavigationBar="false"应用于所有您不想显示导航栏的页面,就像我在 TestPage1.xaml 中所做的那样。将其应用于您的<TabbedPage>(在这种情况下为 RootPage.xaml )将无法正常工作。

答案 2 :(得分:0)

public partial class Home : TabbedPage
{
    public Home()
    {
        var alphaPage = new NavigationPage(new ExistingAlphaPage());
        alphaPage .Title = "Alpha";

        var betaPage = new NavigationPage(new ExistingBetaPage());
        betaPage.Title = "Beta";

        var gamaPage = new NavigationPage(new ExistingGamaPage());
        gamaPage .Title = "Gama";

        Children.Add(alphaPage);
        Children.Add(betaPage);
        Children.Add(gamaPage);
    }
}