App.xaml.cs文件(默认为App.xaml文件,因此已跳过)
using Xamrin.Forms namespace NavigationApp { public partial class App : Application { public static INavigation GlobalNavigation { get; private set; } public App() { InitializeComponent(); var rootPage = new NavigationPage(new FirstPage()); GlobalNavigation = rootPage.Navigation; MainPage = rootPage; } } }
FirstPage.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="NavigationApp.FirstPage" Title="First page"> <ContentPage.Content> <StackLayout> <Label Text="This is the first page" /> <Button Text="Click to navigate to a new page" Clicked="GoToSecondPageButtonClicked"/> <Button Text="Click to open the new page as modal" Clicked="OpenGlobalModalPageButtonClicked"/> </StackLayout> </ContentPage.Content> </ContentPage>
在某些情况下,您需要不在当前导航中而是在全局导航中打开新页面。例如,如果您的当前页面包含底部菜单,则在当前导航中按下新页面时将显示该菜单。如果需要在隐藏底部菜单和其他当前页面内容的整个可见内容上打开页面,则需要将新页面作为模式推送到全局导航中。请参阅App.GlobalNavigation下面的属性和示例。
FirstPage.xaml.cs文件
using System; using Xamarin.Forms; namespace NavigationApp { public partial class FirstPage : ContentPage { public FirstPage() { InitializeComponent(); } async void GoToSecondPageButtonClicked(object sender, EventArgs e) { await Navigation.PushAsync(new SecondPage(), true); } async void OpenGlobalModalPageButtonClicked(object sender, EventArgs e) { await App.GlobalNavigation.PushModalAsync(new SecondPage(), true); } } }
SecondPage.xaml文件(xaml.cs文件是默认文件,因此已跳过)
<?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="NavigationApp.SecondPage" Title="Second page"> <ContentPage.Content> <Label Text="This is the second page" /> </ContentPage.Content> </ContentPage>