×

iFour Logo

Page Navigation between two pages in Xamarin.Forms

Kapil Panchal - March 04, 2021

Listening is fun too.

Straighten your back and cherish with coffee - PLAY !

  • play
  • pause
  • pause
Page Navigation between two pages in Xamarin.Forms

Xamarin is an open-source platform to develop Cross-platform and multi-platform applications, for example, Android, iOS, Windows. In this platform code sharing concept is used. We can write business logic in a single language but achieve native performance, look and feel on each platform.

In this blog, we will discuss page navigation between two pages in Xamarin.Forms. Navigation is performing switching between two or more pages in the application. Navigation pages manage navigation among the pages using stack-based architecture. When we are using page navigation in our application, the home page's instance should be passed into the constructor of the NavigationPage object.

Navigation.PushAsync(); or Navigation.PushModelAsync(); is used to go to another page of the application. Navigation.PopAsync(); or Navigation.PopModelAsync(); is used to go back to the previous page. PushAsync(Page) Presents a page by asynchronously pushing it onto the navigation stack and PushModelAsync(Page) presents a page modally. PopAsync() removes the top page asynchronously from the navigation stack.

Different platforms provide different APIs and UI for navigation. There are two types of navigation in Xamarin.Forms: Hierarchical Navigation and Model Navigation. The navigation class provides hierarchical navigation. In hierarchical navigation, users can navigate through pages forward and backward. To move from one page to another, an application will push a page to the navigation stack and the application will pop the page to return to the previous page. Xamarin.Forms provide support of model pages. In Model navigation, users have to complete a self-contained task that cannot navigate until the task is completed or canceled.

The prerequisite for performing Page Navigation between pages is Visual Studio 2017 or the upper version. Now, let's create an application to perform Page Navigation. These steps are followed to create Page Navigation Application.

Click on Create a new project to initiate a new Xamarin.Forms project.

A one-stop solution to  hire WPF developer  for your esteemed business.

Blog

Now select Mobile App (Xamarin.Forms) and click on the Next button.

ai-Hiring-banner

Give appropriate project name and click on Create button.

ai-Hiring-banner

After that select template for the application and platform and click on Create button.

ai-Hiring-banner

Now, add new page go to the solution explorer >> Right click on project name >> Add >> New Item

 

ai-Hiring-banner

Select Content Page and give a name to the page and click on Add button.

 

ai-Hiring-banner

Looking to build secure applications for your business? Hire ethereum blockchain developer from us right now.

Add one more page as added in the last two steps.

Now, go to the First page (NPage1) and add button Control to the page. Create Clicked Event on this button. Add this code to XAML page:]

  

                
  

              

Now, in the code behind the file add this code on the Button_Clicked_1() method.

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

Navigation.PushAsync(); will navigate us to Page2.

Add this code to the second XAML page (Page2). This page will open when we will click on the button created in NPage1.

     

                
                
                
  

              

Now go to the App.xaml.cs file to set the first page or home page of our application. Add this code to the App () method.

   MainPage = new NavigationPage(new NPage1());
 

NavigationPage() initializes new NavigationPage object. NavigationPage(Page) creates new NavigationPage element with root. It is the root element of NavigationPage. In the above line, we have set NPage1 as a root element. When we will run our application our first page will be NPage1.

ai-Hiring-banner

Image: Page NPage1.

Planning to Hire Xamarin App Development Company ? Your Search ends here.

When we click on GO TO NEXT PAGE button it will navigate to Page2.

ai-Hiring-banner

Image: Page Page2.

This way we can perform Page Navigation between two pages. In the next example, we will perform navigation between two pages- Login Page to next page. We will follow the same steps to perform navigation as described above.

We will create two XAML pages named NPage1 and NPage2. In these two pages, we will create controls like label, Entry and, button. In the Code behind file, we will do navigation on the button click method and in App.xaml.cs file we will set our root page of the application.

NPage1 (xaml file)

          

                
                
                
                
                
                
                
          
                
    
  

        

NPage1.xaml.cs (Code behind file)

  using System;
  using Xamarin.Forms;
  using Xamarin.Forms.Xaml;
          
  namespace PageNavigation
  {
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class NPage1 : ContentPage
     {
       public NPage1()
         {
            InitializeComponent();
         }
          
      async private void Button_Clicked(object sender, EventArgs e)
        {
           await Navigation.PushAsync(new NPage2());
         }
    }
   }
        

ai-Hiring-banner

Image: Page NPage1.

NPage2 (xaml file)

          

                
                
                
  

        

App.xaml.cs

namespace PageNavigation
{
    public partial class App : Application
    {
        public App()
        {
            InitializeComponent();

            MainPage = new NavigationPage(new NPage1());
        }

        protected override void OnStart()
        {
        }

        protected override void OnSleep()
        {
        }

        protected override void OnResume()
        {
        }
    }
}
        

ai-Hiring-banner

Image: Page NPage1.

ai-Hiring-banner

Image: Page NPage2.

Planning to hire Dot Net Core developer for your company project?

Here, we have run this application on the Android platform as we know Xamarin is a platform where we can develop the cross-platform and multi-platform application so we can also run this application in UWP and iOS platforms. For that, we have to add a reference to the platforms.

Conclusion


In Xamarin.Forms NavigationPage class is used to perform navigation between the pages. Navigation means switching from one page to another page of our application. NavigationPage adds or removes the content of the page that we have push or pop. In Xamarin.Forms Page Navigation uses Stack-based architecture. Navigation.PushAsync() and Navigation.PopAsync() is used to perform navigation. PushAsync() is used to go next page, it adds a page to the top of the navigation stack and PopAsync() is used to go back to the previous page, it removes the most recent page from the application. In this blog, we have seen how to perform navigation and used PageNavigation and Navigation.PushAsync().

Page Navigation between two pages in Xamarin.Forms Xamarin is an open-source platform to develop Cross-platform and multi-platform applications, for example, Android, iOS, Windows. In this platform code sharing concept is used. We can write business logic in a single language but achieve native performance, look and feel on each platform. In this blog, we will discuss page navigation between two pages in Xamarin.Forms. Navigation is performing switching between two or more pages in the application. Navigation pages manage navigation among the pages using stack-based architecture. When we are using page navigation in our application, the home page's instance should be passed into the constructor of the NavigationPage object. Navigation.PushAsync(); or Navigation.PushModelAsync(); is used to go to another page of the application. Navigation.PopAsync(); or Navigation.PopModelAsync(); is used to go back to the previous page. PushAsync(Page) Presents a page by asynchronously pushing it onto the navigation stack and PushModelAsync(Page) presents a page modally. PopAsync() removes the top page asynchronously from the navigation stack. Different platforms provide different APIs and UI for navigation. There are two types of navigation in Xamarin.Forms: Hierarchical Navigation and Model Navigation. The navigation class provides hierarchical navigation. In hierarchical navigation, users can navigate through pages forward and backward. To move from one page to another, an application will push a page to the navigation stack and the application will pop the page to return to the previous page. Xamarin.Forms provide support of model pages. In Model navigation, users have to complete a self-contained task that cannot navigate until the task is completed or canceled. The prerequisite for performing Page Navigation between pages is Visual Studio 2017 or the upper version. Now, let's create an application to perform Page Navigation. These steps are followed to create Page Navigation Application. Click on Create a new project to initiate a new Xamarin.Forms project. A one-stop solution to  hire WPF developer  for your esteemed business. Contact us now Now select Mobile App (Xamarin.Forms) and click on the Next button. Give appropriate project name and click on Create button. Read More: Xamarin.essentials: With Permission, App Theme, And Authentication After that select template for the application and platform and click on Create button. Now, add new page go to the solution explorer >> Right click on project name >> Add >> New Item   Select Content Page and give a name to the page and click on Add button.   Looking to build secure applications for your business? Hire ethereum blockchain developer from us right now. Contact now Add one more page as added in the last two steps. Now, go to the First page (NPage1) and add button Control to the page. Create Clicked Event on this button. Add this code to XAML page:] Now, in the code behind the file add this code on the Button_Clicked_1() method. async private void Button_Clicked_1(object sender, EventArgs e) { await Navigation.PushAsync(new Page2()); } Navigation.PushAsync(); will navigate us to Page2. Add this code to the second XAML page (Page2). This page will open when we will click on the button created in NPage1. Now go to the App.xaml.cs file to set the first page or home page of our application. Add this code to the App () method. MainPage = new NavigationPage(new NPage1()); NavigationPage() initializes new NavigationPage object. NavigationPage(Page) creates new NavigationPage element with root. It is the root element of NavigationPage. In the above line, we have set NPage1 as a root element. When we will run our application our first page will be NPage1. Image: Page NPage1. Planning to Hire Xamarin App Development Company ? Your Search ends here. Contact now When we click on GO TO NEXT PAGE button it will navigate to Page2. Image: Page Page2. This way we can perform Page Navigation between two pages. In the next example, we will perform navigation between two pages- Login Page to next page. We will follow the same steps to perform navigation as described above. We will create two XAML pages named NPage1 and NPage2. In these two pages, we will create controls like label, Entry and, button. In the Code behind file, we will do navigation on the button click method and in App.xaml.cs file we will set our root page of the application. NPage1 (xaml file) NPage1.xaml.cs (Code behind file) using System; using Xamarin.Forms; using Xamarin.Forms.Xaml; namespace PageNavigation { [XamlCompilation(XamlCompilationOptions.Compile)] public partial class NPage1 : ContentPage { public NPage1() { InitializeComponent(); } async private void Button_Clicked(object sender, EventArgs e) { await Navigation.PushAsync(new NPage2()); } } } Image: Page NPage1. NPage2 (xaml file) App.xaml.cs namespace PageNavigation { public partial class App : Application { public App() { InitializeComponent(); MainPage = new NavigationPage(new NPage1()); } protected override void OnStart() { } protected override void OnSleep() { } protected override void OnResume() { } } } Image: Page NPage1. Image: Page NPage2. Planning to hire Dot Net Core developer for your company project? Reach out us Here, we have run this application on the Android platform as we know Xamarin is a platform where we can develop the cross-platform and multi-platform application so we can also run this application in UWP and iOS platforms. For that, we have to add a reference to the platforms. Conclusion In Xamarin.Forms NavigationPage class is used to perform navigation between the pages. Navigation means switching from one page to another page of our application. NavigationPage adds or removes the content of the page that we have push or pop. In Xamarin.Forms Page Navigation uses Stack-based architecture. Navigation.PushAsync() and Navigation.PopAsync() is used to perform navigation. PushAsync() is used to go next page, it adds a page to the top of the navigation stack and PopAsync() is used to go back to the previous page, it removes the most recent page from the application. In this blog, we have seen how to perform navigation and used PageNavigation and Navigation.PushAsync().
Kapil Panchal

Kapil Panchal

A passionate Technical writer and an SEO freak working as a Content Development Manager at iFour Technolab, USA. With extensive experience in IT, Services, and Product sectors, I relish writing about technology and love sharing exceptional insights on various platforms. I believe in constant learning and am passionate about being better every day.

Build Your Agile Team

Enter your e-mail address Please enter valid e-mail

Categories

Ensure your sustainable growth with our team

Talk to our experts
Sustainable
Sustainable
 
Blog Our insights
13 Ways Power Apps Simplifies eDiscovery
13 Ways Power Apps Simplifies eDiscovery

E-Discovery is a crucial process for legal research enabling lawyers to find the digital evidence they need. It involves finding, collecting, and filtering e-data related to their...

Top Data Analytics Trends You Can't Ignore
Top Data Analytics Trends You Can't Ignore

Can you believe that 147 zettabytes of data have already been created in 2024, and guess what? It is anticipated to be 180 zettabytes by 2025 (according to Statista). Now just think...

Why Use Power Apps for Case Management – 11 Reasons
Why Use Power Apps for Case Management – 11 Reasons

It’s amazing to witness that legal consultants who once clung to print documents have now embraced modern technologies for their legal work. In fact, a recent survey revealed that over 72% of law firms employ cloud-based technologies for managing case files, scheduling, and billing. This shift is not just about convenience; it’s about the progress we observe in the legal field.