【问题标题】:Open new Page withing a tab-navigation在选项卡导航中打开新页面
【发布时间】:2016-10-30 20:21:56
【问题描述】:

我正在使用标签导航和页面,并想打开一个新页面。

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

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

在 RootPage 中,我填写了 Navigation:

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 等其他应用中,它们的效果相同,但看起来更流畅。

有没有什么办法可以让tab-navigation在back-navigation之间切换更流畅?

感谢您的帮助:)

【问题讨论】:

    标签: c# xaml xamarin navigation tabbedpage


    【解决方案1】:

    如果你想在一个标签内导航,每个标签都应该有它自己的 NavigationPage。 TabbedPage 本身不应位于 NavigationPage 中。

    MainPage = new RootPage();
    

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

    【讨论】:

    • 你好,我有一个小问题。我有一个登录、注册和仪表板。仪表板已经是选项卡式页面,它位于导航页面中,因为根页面是您必须选择登录或注册的主页
    【解决方案2】:

    正如 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 中所做的那样。将其应用于您的 &lt;TabbedPage&gt;,在这种情况下为 RootPage.xaml,将不起作用。

    【讨论】:

    • 你好,我有一个小问题。我有一个登录、注册和仪表板。仪表板已经是选项卡式页面,它位于导航页面中,因为根页面是您必须选择登录或注册的主页
    【解决方案3】:
    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);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-23
      • 2020-03-14
      • 2021-06-19
      • 1970-01-01
      • 2020-09-02
      • 2017-01-05
      • 1970-01-01
      • 2013-08-22
      相关资源
      最近更新 更多