页面跳转

简介

Xamarin.Forms提供了许多不同的页面导航体验,具体取决于所使用的页面类型
对于ContentPage实例,有两种导航体验:

Hierarchical Navigation
Modal Navigation

CarouselPage,MasterDetailPage和TabbedPage类提供了替代的导航体验
更多资料:https://developer.xamarin.com/guides/xamarin-forms/application-fundamentals/navigation/

Hierarchical Navigation

NavigationPage类提供了一种分层导航体验,用户可以根据需要在页面中进行导航,向前和向后导航
该类实现导航作为Page对象的后进先出(LIFO)堆栈
在分层导航中,NavigationPage类用于浏览一堆ContentPage对象
要从一个页面移动到另一个页面,应用程序会将新页面推到导航堆栈上,在那里它将成为活动页面
要返回到上一页面,应用程序将从导航堆栈中弹出当前页面,并且新的最顶端页面将成为活动页面

根页面设置为NavigationPage

添加到导航堆栈的第一个页面被称为应用程序的根页面
在App中修改起始页的设置

MainPage = new NavigationPage(new MainPage());

跳转页面

使用Navigation.PushAsync()方法

Button StackLayoutDemo1Button = new Button();
StackLayoutDemo1Button.Clicked += ((sender,e)=>
{
    Navigation.PushAsync(new StackLayoutExample());
});
StackLayoutDemo1Button.Text = "StackLayout+Label";

//内容
Content = new StackLayout
{
    //间距
    Spacing = 10,
    Children =
    {
        StackLayoutDemo1Button
    }
};

返回上一页

使用Navigation.PopAsync()方法

var backButton = new Button();
backButton.Text = "返回";
backButton.Clicked += ((sender,e) =>
{
    Navigation.PopAsync();
});

//内容
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.
执行Modal Navigation不需要NavigationPage的实例

跳转页面

使用Navigation.PushModalAsync()方法

StackLayoutDemo1Button.Text = "StackLayout+Label";
Button StackLayoutDemo1Button2 = new Button();
StackLayoutDemo1Button2.Clicked += ((sender,e)=> {
    Navigation.PushModalAsync(new ListViewInStackLayout());
});
StackLayoutDemo1Button2.Text = "StackLayout+ListView";

//内容
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

你可能感兴趣的:(页面跳转)