PushAsync is not supported globally on Android, please use a NavigationPage - Xamarin.Forms

88,234

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:

enter image description here

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());
    }
}
Share:
88,234
Michael Kniskern
Author by

Michael Kniskern

I am currently working as an IT engineer for the government of Mesa, Arizona USA

Updated on November 24, 2021

Comments

  • Michael Kniskern
    Michael Kniskern over 2 years

    I have the following method in an Xamarin.Forms.ContentPage wired to a button click event

    public 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
    Michael Kniskern almost 10 years
    Would I enclose my LoginPage within a NavigationPage in my App.GetMainPage method? Also, could I use the 'Navigation.PopAsync()' method to remove the loginpage from the navigation stack?
  • Jason
    Jason almost 10 years
    Yes, 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
    Stephane Delcroix almost 10 years
    Other approach is to have the LoginPage as main page for the application, and PushModalAsync a new NavigationPage with the content of the app on successful login.
  • Chase Florell
    Chase Florell almost 10 years
    The 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
    Alessandro Caliaro almost 8 years
    I 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
    Anwar over 7 years
    Tryied everthing in vain, this worked like a charm (build Android 6, emulated on LG G2 Android 4.4).
  • hamdanjz4
    hamdanjz4 about 7 years
    I preferred to use this approach instead of using some "isLoggedIn" logic in App.cs to show MainPage or LoginPage
  • Muhammad Ramzan
    Muhammad Ramzan about 7 years
    Thanks @Reader Man San
  • GummyBear21
    GummyBear21 over 6 years
    Work like a charm. Thanks
  • Eldlabs
    Eldlabs about 6 years
    You don't need to use new NaigationPage() inside PushAsync. You can have await Navigation.PushAsync(new MainPage2()); A bit less code :)
  • Shweta Nandha
    Shweta Nandha about 6 years
    MainPage = new NavigationPage(new PageA()); working with App but not working with ContentPage
  • Shweta Nandha
    Shweta Nandha about 6 years
    await 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
    Ola Ström about 4 years
    This mess up the design since it adds an navigation bar in the top of the page.
  • Ola Ström
    Ola Ström about 4 years
    This mess up the design since it adds an navigation bar in the top of the page.
  • quemeful
    quemeful about 3 years
    someone should make an IDE that pairs error warnings with top voted StackOverflow answers
  • abhi
    abhi over 2 years
    The Navigation Bar is what our users desire to see. :)
  • abhi
    abhi over 2 years
    PushModalAsync works, but it also gets rid of the Navigation bar at the top, that our users want to see.