Xamarin.form Shell PopAsync vs Backward Navigation?

Emil Alipiev 306 Reputation points
2021-05-25T21:50:55.277+00:00

What is the difference between

await Shell.Current.Navigation.PopAsync();

vs

await Shell.Current.GoToAsync("..");

When to use which one?

Developer technologies | .NET | Xamarin
0 comments No comments
{count} vote

Accepted answer
  1. Kyle Wang 5,531 Reputation points Microsoft External Staff
    2021-05-26T02:55:44.087+00:00

    Hi EmilAlipiev-5934,

    Welcome to our Microsoft Q&A platform!

    Xamarin uses a navigation stack to hold the navigation pages. According to the documentation: INavigation.PopAsync Method, we can know that this method will pop the top page from the navigation stack. In other words, it can only pop one page at a time. Correspondingly, there is also an INavigation.PushAsync method to add a page to the stack, i.e., open a new page on the screen. Also, there is an INavigation.PopToRootAsync method which will pop all the extra pages and show the root page.

    Now, assume that you have three pages: RootPage, MiddlePage and LastPage.

    // RootPage.xaml.cs  
    // open MiddlePage  
    Shell.Current.Navigation.PushAsync(new MiddlePage());  
      
    // MiddlePage.xaml.cs  
    // open LastPage  
    Shell.Current.Navigation.PushAsync(new LastPage());  
      
    // LastPage.xaml.cs  
    // go back to MiddlePage  
    Shell.Current.Navigation.PopAsync();  
    // or go back to RootPage  
    Shell.Current.Navigation.PopToRootAsync();  
    

    As to Shell.Current.GoToAsync, it is used to navigate to a specified page based on URI. Before using it, you need to register the route.

    Routing.RegisterRoute(nameof(RootPage), typeof(RootPage));  
    Routing.RegisterRoute(nameof(MiddlePage), typeof(MiddlePage));  
    Routing.RegisterRoute(nameof(LastPage), typeof(LastPage));  
    
    // RootPage.xaml.cs  
    // open MiddlePage  
    await Shell.Current.GoToAsync($"{nameof(MiddlePage)}");  
          
    // MiddlePage.xaml.cs  
    // open LastPage  
    await Shell.Current.GoToAsync($"{nameof(LastPage)}");  
          
    // LastPage.xaml.cs  
    // go back to MiddlePage  
    await Shell.Current.GoToAsync($"{nameof(MiddlePage)}");  
    // or  
    await Shell.Current.GoToAsync("..");  
    // or go back to RootPage  
    await Shell.Current.GoToAsync($"{nameof(RootPage)}");  
    

    And you can navigate to a page using "absolute routes", such as,

    await Shell.Current.GoToAsync($"{nameof(MiddlePage)}/{nameof(LastPage)}");  
    

    It will show the "LastPage", and if you click the back button in the upper left corner, it will open "MiddlePage".

    For more info, you can refer to Xamarin.Forms Shell navigation.

    Best regards,
    Kyle


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.