簡介
Xamarin.Forms提供了許多不同的頁面導航體驗膜廊,具體取決于所使用的頁面類型
對于ContentPage實例患蹂,有兩種導航體驗:
Hierarchical Navigation
Modal Navigation
CarouselPage作瞄,MasterDetailPage和TabbedPage類提供了替代的導航體驗
更多資料:https://developer.xamarin.com/guides/xamarin-forms/application-fundamentals/navigation/
Hierarchical Navigation
NavigationPage類提供了一種分層導航體驗城菊,用戶可以根據(jù)需要在頁面中進行導航坛掠,向前和向后導航
該類實現(xiàn)導航作為Page對象的后進先出(LIFO)堆棧
在分層導航中,NavigationPage類用于瀏覽一堆ContentPage對象
要從一個頁面移動到另一個頁面成箫,應用程序會將新頁面推到導航堆棧上,在那里它將成為活動頁面
要返回到上一頁面旨枯,應用程序?qū)膶Ш蕉褩V袕棾霎斍绊撁娴挪⑶倚碌淖铐敹隧撁鎸⒊蔀榛顒禹撁?/p>
根頁面設置為NavigationPage
添加到導航堆棧的第一個頁面被稱為應用程序的根頁面
在App中修改起始頁的設置
MainPage = new NavigationPage(new MainPage());
跳轉(zhuǎn)頁面
使用Navigation.PushAsync()方法
Button StackLayoutDemo1Button = new Button();
StackLayoutDemo1Button.Clicked += ((sender,e)=>
{
Navigation.PushAsync(new StackLayoutExample());
});
StackLayoutDemo1Button.Text = "StackLayout+Label";
//內(nèi)容
Content = new StackLayout
{
//間距
Spacing = 10,
Children =
{
StackLayoutDemo1Button
}
};
返回上一頁
使用Navigation.PopAsync()方法
var backButton = new Button();
backButton.Text = "返回";
backButton.Clicked += ((sender,e) =>
{
Navigation.PopAsync();
});
//內(nèi)容
Content = new StackLayout
{
Spacing = 10,
Children = { backButton }
};
示例代碼
https://github.com/zLulus/NotePractice/tree/dev3/Xamarin.Forms/XamarinDemo/XamarinDemo/XamarinDemo
從MainPage->StackLayoutExample
Modal Navigation
A NavigationPage instance is not required for performing modal page navigation.
執(zhí)行Modal Navigation不需要NavigationPage的實例
跳轉(zhuǎn)頁面
使用Navigation.PushModalAsync()方法
StackLayoutDemo1Button.Text = "StackLayout+Label";
Button StackLayoutDemo1Button2 = new Button();
StackLayoutDemo1Button2.Clicked += ((sender,e)=> {
Navigation.PushModalAsync(new ListViewInStackLayout());
});
StackLayoutDemo1Button2.Text = "StackLayout+ListView";
//內(nèi)容
Content = new StackLayout
{
//間距
Spacing = 10,
Children =
{
StackLayoutDemo1Button2
}
};
返回上一頁
使用Navigation.PopModalAsync()方法
var backButton = new Button();
backButton.Text = "返回";
backButton.Clicked += ((sender, e) =>
{
Navigation.PopModalAsync();
});
Content = new StackLayout
{
VerticalOptions = LayoutOptions.FillAndExpand,
Children = { backButton }
};
示例代碼
https://github.com/zLulus/NotePractice/tree/dev3/Xamarin.Forms/XamarinDemo/XamarinDemo/XamarinDemo
從MainPage->ListViewInStackLayout