Navigation betweens pages

jvallejvalle Member ✭✭
edited January 19 in Xamarin.Forms

Hello to everyone

I am trying to navigate between two pages,** first:**

private void Page2_Clicked(object sender, EventArgs e)
{
Navigation.PushAsync(new Page2());
}

Second

async private void Page2_Clicked(object sender, EventArgs e)
{
await Navigation.PushAsync(new Page2());
}

And both send me the follow Error:

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

how can I solve it

Best Answers

  • jezhjezh Xamurai
    edited January 20 Accepted Answer

    When you go to current page(eg. MainPage), you can code as follows:

       MainPage = new NavigationPage(new MainPage());
    

    instead of

       MainPage = new MainPage();
    

    So the code of App .cs

      public partial class App : Application
        {
            public App()
            {
                InitializeComponent();
    
                // MainPage = new MainPage();
                MainPage = new NavigationPage(new MainPage());
    
            }
    
            protected override void OnStart()
            {
            }
    
            protected override void OnSleep()
            {
            }
    
            protected override void OnResume()
            {
            }
        }
    

    After that, you can navigation from current page(MainPage ) to Page2 like this just as you used in your code:

        private  void Button_Clicked(object sender, EventArgs e)
            {
                Navigation.PushAsync(new SecondPage());    
            }
    

    OR

       private async void Button_Clicked(object sender, EventArgs e)
            {
                await Navigation.PushAsync(new SecondPage());    
            }
    

Answers

  • jezhjezh Member, Xamarin Team Xamurai
    edited January 20 Accepted Answer

    When you go to current page(eg. MainPage), you can code as follows:

       MainPage = new NavigationPage(new MainPage());
    

    instead of

       MainPage = new MainPage();
    

    So the code of App .cs

      public partial class App : Application
        {
            public App()
            {
                InitializeComponent();
    
                // MainPage = new MainPage();
                MainPage = new NavigationPage(new MainPage());
    
            }
    
            protected override void OnStart()
            {
            }
    
            protected override void OnSleep()
            {
            }
    
            protected override void OnResume()
            {
            }
        }
    

    After that, you can navigation from current page(MainPage ) to Page2 like this just as you used in your code:

        private  void Button_Clicked(object sender, EventArgs e)
            {
                Navigation.PushAsync(new SecondPage());    
            }
    

    OR

       private async void Button_Clicked(object sender, EventArgs e)
            {
                await Navigation.PushAsync(new SecondPage());    
            }
    
Sign In or Register to comment.