如何在 Xamarin 中导航页面?

HQt*_*izy 0 xaml xamarin.ios xamarin xamarin.forms

我在 Xaml 中有这个代码:

<Button Text="Click Me" Clicked="OnButtonClicked" />
Run Code Online (Sandbox Code Playgroud)

代码隐藏中的这段代码:

void OnButtonClicked(object sender, EventArgs args)
{

}
Run Code Online (Sandbox Code Playgroud)

如何使用此方法导航页面?

Rob*_*iel 5

首先,请确保您的应用基于NavigationPage在 Xamarin.Forms 中启用导航。为此,请将MainPage您的属性设置App.csNaviationPage实例。

public App()
{           
    MainPage = new NavigationPage(new YourStartPage());
}
Run Code Online (Sandbox Code Playgroud)

现在添加第二个Forms Xaml PageForms ContentPage到您的项目,您可以从起始页导航到该项目。确保您的OnButtonClicked处理程序声明为async(如下例所示)并按如下方式导航:

async void OnButtonClicked(object sender, EventArgs args)
{
    await Navigation.PushAsync(new Page2());
}   
Run Code Online (Sandbox Code Playgroud)