简体   繁体   中英

How can I change the Page in Xamarin.Forms?

I am on FirstPage and want to change to SecondPage by calling

await Navigation.PushAsync(new SecondPage());

But I always get an Exception. How do I navigate?

PushAsync is for use in a NavigationPage which handles Navigation for you. For this create your MainPage in your App class like this:

MainPage = new NavigationPage(new FirstPage());

Then you can use PushAsync.

If you don't want to use a NavigationPage for certain reasons, you should use

await Navigation.PushModalAsync(new SecondPage());

With this you have something like a two-dimensional navigation stack.

You need to set your First Page as your main page.

MainPage=new NavigationPage(new FirstPage());

And from the FirstPage you can go to the second page using a click event

btnTest.Clicked += async (sender, e) =>
{
    await Navigation.PushAsync(new SecondPage());
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM