How does PopAsyn work under the hood ?

Dani_S 2,886 Reputation points
2023-04-23T07:27:33.4066667+00:00

Hi, await App.Current.MainPage.Navigation.PopAsync(true); How does PopAsyn work under the hood ? Does it render the previous page again ? Thanks,

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
2,989 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,401 questions
{count} votes

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 69,541 Reputation points Microsoft Vendor
    2023-04-24T05:26:25.95+00:00

    Hello,

    ו have login page which go to main page and later from main page I go to other pages and go back.

    You can refer to following steps to navigate your page. Firstly, if the first page is login page. You can set the loginpage as the MainPage in App.xaml.cs like following code.

    public App()
        {
            InitializeComponent();
           MainPage =new LoginPage();
        }
    

    Then, Open your LoginPage, if you click the login button and navigate to the MainPage, you can use App.Current.MainPage = new NavigationPage(new MainPage()); to redefine the MainPage.

    public partial class LoginPage : ContentPage
    {
        public LoginPage()
        {
            InitializeComponent();
        }
    
       private void Button_Clicked(object sender, EventArgs e)
        {
            App.Current.MainPage = new NavigationPage(new MainPage());
        }
    }
    

    Next, you can navigate the page by Navigation.PushAsync in the MainPage or other navigated pages. For example, I want to navigate to the NewPage2 in the Button click event of the Mainpage. I can use following code to navigate it.

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

    In the end, you can navigate to the previous page by Navigation.PopAsync();. For example, If you want to navigate to the MainPage in the NewPage2's button click event, you can use following code.

     private void Button_Clicked(object sender, EventArgs e)
        {       
            Navigation.PopAsync();
        }
    

    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 comments No comments

1 additional answer

Sort by: Most helpful
  1. Alexander Petree 150 Reputation points
    2023-04-24T04:59:48.5066667+00:00
    [text]()
    

    public App()     {         InitializeComponent();        MainPage =new NavigationPage(new MainPage());     }

    0 comments No comments