PushAsync is not supported globally on Android, please use a NavigationPage - Xamarin.Forms
Solution 1
You are calling "PushAsync":
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
}
private void btnCourseList_Clicked(object sender, EventArgs e)
{
Navigation.PushAsync(new PageB());
}
}
but you did not start the NavigationPage, which normally is done in the App.cs class, or at least it should be started before any call to "PushAsync":
MainPage = new NavigationPage(new PageA());
Solution 2
In app.xaml.cs file,
Replace
MainPage = new <namespace>.MainPage();
With
MainPage = new NavigationPage(new <namespace>.MainPage());
Then Use
await Navigation.PushAsync(new NavigationPage(new MainPage2()));
Solution 3
You need to enclose your LoginPage in a NavigationPage. This will fix your error, but will leave you with the LoginPage contained on your navigation stack.
An alternate approach would be to make your HomePage the root of the application, then display the LoginPage modally on top of it. Only when the user successfully logs in do you dismiss the LoginPage modal so they can see the HomePage.
Solution 4
First make setting in "Main App Page" then do in "Content page" to go other page:
Solution 5
I only change pushAsync with pushModalAsync :)
public async void LogIn(object sender, EventArgs eventsArgs)
{
//do authenticate stuff here
SSO.MyAuthentication client = new SSO.MyAuthentication();
bool isAuthenticated = client.Authenticate(_UsernameInput.Text, _PasswordInput.Text);
if(isAuthenticated)
{
//Push home page to top of navigation stack
//Navigation.PushAsync(new HomePage());
Navigation.PushModalAsync(new HomePage());
}
}
Michael Kniskern
I am currently working as an IT engineer for the government of Mesa, Arizona USA
Updated on November 24, 2021Comments
-
Michael Kniskern over 2 years
I have the following method in an
Xamarin.Forms.ContentPage
wired to a button click eventpublic class LoginPage : ContentPage { private Button _loginButton = null; private Entry _PasswordInput = null; private Entry _UsernameInput = null; public LoginPage() { _UsernameInput = new Entry { Placeholder = "Username" }; _PasswordInput = new Entry { Placeholder = "Password", IsPassword = true }; _loginButton = new Button { Text = "Login", BorderRadius = 5 } _loginButton.Clicked += LogIn; Content = new StackLayout { VerticalOptions = LayoutOptions.Center, Children = { _UsernameInput, _PasswordInput, _loginButton, }, Spacing = 15 }; } public async void LogIn(object sender, EventArgs eventsArgs) { //do authenticate stuff here SSO.MyAuthentication client = new SSO.MyAuthentication(); bool isAuthenticated = client.Authenticate(_UsernameInput.Text, _PasswordInput.Text); if(isAuthenticated) { //Push home page to top of navigation stack Navigation.PushAsync(new HomePage()); } } }
On the following line of code
Navigation.PushAsync(new HomePage());
, I am getting the following exception while debugging:PushAsync is not supported globally on Android, please use a NavigationPage
How do I resolve this issue using a
Xamarin.Forms.NavigationPage
object? -
Michael Kniskern almost 10 yearsWould I enclose my
LoginPage
within aNavigationPage
in myApp.GetMainPage
method? Also, could I use the 'Navigation.PopAsync()' method to remove the loginpage from the navigation stack? -
Jason almost 10 yearsYes, you make NavPage your outermost page. I don't think you can use PopAsync() when there is only one page on the stack, but I haven't tried it.
-
Stephane Delcroix almost 10 yearsOther approach is to have the
LoginPage
as main page for the application, andPushModalAsync
a newNavigationPage
with the content of the app on successful login. -
Chase Florell almost 10 yearsThe best approach is to load the app normally and
PushModalAsync()
the login page. Just be sure to prevent the user from pressing the hardware back button on Android.public override void OnBackPressed(){ if(user.IsAuthenticated(){base.OnBackPressed();}}
-
Alessandro Caliaro almost 8 yearsI have the same problem when I try to call PushAsync in a Modal Page. Something like - PushAsync - PushModalAsync - PushAsync (here I have the problem and your workarounds does not work)
-
Anwar over 7 yearsTryied everthing in vain, this worked like a charm (build Android 6, emulated on LG G2 Android 4.4).
-
hamdanjz4 about 7 yearsI preferred to use this approach instead of using some "isLoggedIn" logic in App.cs to show MainPage or LoginPage
-
Muhammad Ramzan about 7 yearsThanks @Reader Man San
-
GummyBear21 over 6 yearsWork like a charm. Thanks
-
Eldlabs about 6 yearsYou don't need to use new NaigationPage() inside PushAsync. You can have await Navigation.PushAsync(new MainPage2()); A bit less code :)
-
Shweta Nandha about 6 yearsMainPage = new NavigationPage(new PageA()); working with App but not working with ContentPage
-
Shweta Nandha about 6 yearsawait Navigation.PushAsync(new MainPage()); not working with ContentPage i am getting System.InvalidOperationException: PushAsync is not supported globally on Android, please use a NavigationPage.
-
Ola Ström about 4 yearsThis mess up the design since it adds an navigation bar in the top of the page.
-
Ola Ström about 4 yearsThis mess up the design since it adds an navigation bar in the top of the page.
-
quemeful about 3 yearssomeone should make an IDE that pairs error warnings with top voted StackOverflow answers
-
abhi over 2 yearsThe Navigation Bar is what our users desire to see. :)
-
abhi over 2 yearsPushModalAsync works, but it also gets rid of the Navigation bar at the top, that our users want to see.