question

EmilAlipiev-5934 avatar image
1 Vote"
EmilAlipiev-5934 asked KyleWang-MSFT edited

Xamarin.form Shell PopAsync vs Backward Navigation?

What is the difference between

await Shell.Current.Navigation.PopAsync();

vs

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

When to use which one?

dotnet-xamarin
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

KyleWang-MSFT avatar image
1 Vote"
KyleWang-MSFT answered KyleWang-MSFT edited

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.


· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks for the whole information but it is general information. I am already experienced with XF, just not with Shell. my concrete question is how different is

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

than

Shell.Current.Navigation.PopAsync();

Because both are going back to previous page.

1 Vote 1 ·

@EmilAlipiev-5934 If you just want to go back to the previous page, I think there is no difference between them.

0 Votes 0 ·