Please tell me, the architecture of the application on Xamarin Forms is such that the user consistently switches from one page to another.

With the opening of the first page - there were no special problems. In App.xaml.cs added

 MainPage = new NavigationPage(new MainPage()); 

And from the main page MainPage.xaml.cs the user navigates to the list page. Everything is normal here. No problem.

 App.Current.MainPage = new Page1(); // без навигации 

or

 await Navigation.PushAsync(new Page1()); // с навигацией 

or because further when selecting a position from the ListView, the following window should open:

 await Navigation.PushAsync(new Page2(e.SelectedItem.ToString())); 

But, I get this error:

System.InvalidOperationException: PushAsync is not supported globally on Android, please use a NavigationPage.

:(

Re-read a bunch of solutions, tried, but without much success. Many examples consist of only 2 pages. I could not find an example with three or more pages to understand it.

Maybe something I did not consider? Maybe it is not done at all?

I would be very happy to advice! Thank!

  • Do you have a problem only in a project with Android? - Emigrant
  • Yes. While in it. - Andrey

1 answer 1

The problem is that you call

 await Navigation.PushAsync(new Page2(e.SelectedItem.ToString())); 

from page Page1. But PushAsync needs to be called from the root page, that is, the MainPage. I recommend you add a class that controls the navigation. For example,

 public static class NavigationService { private static Application CurrentApplication { get { return Application.Current; } } public static async Task NavigateToAsync(Page page) { if (CurrentApplication.MainPage is NavigationPage navigationPage) { await navigationPage.Navigation.PushAsync(page); } } } 

Then the call to Page 1 will be like this:

 await NavigationService.NavigateToAsync(new Page1()); 

From Page 1 to Page 2:

 await NavigationService.NavigateToAsync(new Page2(e.Item.ToString())); 
  • Thank you very much!!! Everything worked out! :) - Andrey
  • I was glad to help :) - Emigrant