MAUI: What is the difference between GoToAsync() and PushAsync() to navigate between pages?

honggyu kim 20 Reputation points
2024-05-26T10:02:39.0233333+00:00

What is the difference exactly between using

  1. await Shell.Current.GoToAsync
  • Route
  • Relative/GoBack with (..)
  • Single instance??
  1. await Shell.Current.Navigation.PushAsync(Use NavigationPage) - similar to prism or Xamarin Forms NavigateService.PushAsync
  • Can switch pages without GoBack or Relative routing
  • Modal Popup/back
  • Create New instance at every new stack??

Q1) Which way should I use and MS recommend to use? Should I consider the way, Shell.Current.Navigation.PushAsync as old way and I should use GoToAsync way?

Q1.1) Can I do the following

using GoToAsync way?
Step1) Shell.GoToAsync(//PageA/PageB/PageC)
Step2) Shell.GoToAsync(//PageA) then I can call this after the step 1
Step3) Shell.GoToAsync(//PageB/PageC) then I can call this after the step 2

using PushAsync?
Step1) Shell.Current.Navigation.PushAsync(/PageA/PageB/PageC)
Step2) Shell.Current.Navigation.PushAsync(/PageA) then I can call this after the step 1
Step3) Shell.Current.Navigation.PushAsync(/PageB/PageC) then I can call this after the step 2

I can tell GoToAsync way has some limitation like

it dose not Programmatically set the PresentationMode as Modal or Normal or ModalAnimation, I know there is a method SetPresentationMode(this, PresentationMode.Modal), but I would like to Set the Mode before navigation to the target page, so that I can choose in between Modal or Normal View.

For instance) I have two buttons on PageA,
First button navigates to PageB as Modal
Second button navigates to the same PageB as Normal Page

Q2) Is there any way I can do the above?

Thank you for helping me out to keep me working with MAUI in advance.

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,143 questions
0 comments No comments
{count} votes

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 71,596 Reputation points Microsoft Vendor
    2024-05-27T07:45:01.62+00:00

    Hello,

    Is there any way I can do the above?

    You can use Shell.Current.GoToAsync to do it . As you said, you have two button click event.

    The first button click will open NavigatedPage2 with Modal mode, the second button click will open NavigatedPage2 with Normal mode. when execute the GoToAsync, we can transfer a navigationParameter.

    
    private async void Button_Clicked_1(object sender, EventArgs e)
    {
        var navigationParameter = new ShellNavigationQueryParameters
        {
            { "IsModal", true }
         };
     
        await Shell.Current.GoToAsync("NavigatedPage2", navigationParameter);
    }
    
    private async void Button_Clicked(object sender, EventArgs e)
    {
        var navigationParameter = new ShellNavigationQueryParameters
        {
            { "IsModal", false }
         };
     
        await Shell.Current.GoToAsync("NavigatedPage2", navigationParameter);
    }
     
    
    

    In the navigatedPage2, if the value of IsModal is ture, we can set Shell.SetPresentationMode(this, PresentationMode.Modal);. This NavigatedPage2 will be opened by Modal mode.

    [QueryProperty(nameof(IsModal), "IsModal")]
    public partial class NavigatedPage2 : ContentPage
    {
        bool isModal;
        public bool IsModal
        {
            get => isModal;
            set
            {
                isModal = value;
     
                if (isModal)
                {
                    Shell.SetPresentationMode(this, PresentationMode.Modal);
                }
                OnPropertyChanged();
            }
        }
    

    By the way, please keep one thread for one issue.

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    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.


0 additional answers

Sort by: Most helpful