在选项卡导航中打开新页面
Open new Page withing a tab-navigation
我正在使用标签导航和一个页面,想打开一个新页面。
在我的 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 等其他应用程序中,它们也是这样做的,但看起来更流畅。
有没有什么办法可以让后退导航之间的标签导航更顺畅?
感谢您的帮助:)
如果您想在选项卡内导航,每个选项卡都应该有自己的 NavigationPage。 TabbedPage 本身不应在 NavigationPage 中。
MainPage = new RootPage();
和
Children.Add(new NavigationPage(new TestPage3
{
Title = "TestPage3"
}));
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);
}
}
正如 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[ 中所做的那样=36=]。将它应用于您的 <TabbedPage>
,在这种情况下 RootPage.xaml,将不起作用。
我正在使用标签导航和一个页面,想打开一个新页面。
在我的 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 等其他应用程序中,它们也是这样做的,但看起来更流畅。
有没有什么办法可以让后退导航之间的标签导航更顺畅?
感谢您的帮助:)
如果您想在选项卡内导航,每个选项卡都应该有自己的 NavigationPage。 TabbedPage 本身不应在 NavigationPage 中。
MainPage = new RootPage();
和
Children.Add(new NavigationPage(new TestPage3
{
Title = "TestPage3"
}));
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);
}
}
正如 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[ 中所做的那样=36=]。将它应用于您的 <TabbedPage>
,在这种情况下 RootPage.xaml,将不起作用。