Introduction

This article demonstrates how to use the navigation pages to perform navigation in a stack of pages where the user is able to navigate through pages, forward and backward as desired, and the class implements navigation as a last-in, first-out (LIFO) stack of the page object.
  • PushAsync
    Move from one page to another; the application will push a new page onto the navigation stack where it will become the active page.

  • PopAsync
    To return back to the previous page, the application will pop the current page up from the navigation stack, and the new topmost page becomes the active page.

Let's start :)

Step 1
You can create a new Xamarin.Forms app by going to File ->> New -> Visual C# -> Cross Platform >> Cross-Platform App (Xamarin.Native or Xamarin.Forms), give the application name, and press OK.

(Project Name : HierarchicalNavigation)

Step 2

Open Solution Explorer >> HierarchicalNavigation(PCL) >> right click and select "New Item". In the popup window, select Cross Platform ->> Class.
This way, you can add a new class. Create a new Class named MainPage1, double-click to get into its design view, and insert the code given below.

CS code
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Xamarin.Forms;
  7. namespace NavigtionHirechical
  8. {
  9. class MainPage1 : ContentPage
  10. {
  11. public MainPage1()
  12. {
  13. var nextPageButton = new Button { Text = "Next Page", VerticalOptions = LayoutOptions.CenterAndExpand };
  14. nextPageButton.Clicked += NextPageButton_Clicked;
  15. Title = "MainPage";
  16. Content = new StackLayout
  17. {
  18. Children =
  19. {
  20. nextPageButton
  21. }
  22. };
  23. }
  24. async void NextPageButton_Clicked(object sender, EventArgs e)
  25. {
  26. await Navigation.PushAsync (new MainPage2());
  27. }
  28. }
  29. }
Step 3

Similarly, create another class named MainPage2 and add the following code.

CS Code
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Xamarin.Forms;
  7. namespace NavigtionHirechical
  8. {
  9. public class MainPage2 : ContentPage
  10. {
  11. public MainPage2()
  12. {
  13. var nextPageButton = new Button { Text = "Next MainPage3", VerticalOptions = LayoutOptions.CenterAndExpand };
  14. nextPageButton.Clicked += OnNextPageButtonClicked;
  15. var previousPageButton = new Button { Text = "Previous MainPage1", VerticalOptions = LayoutOptions.CenterAndExpand };
  16. previousPageButton.Clicked += OnPreviousPageButtonClicked;
  17. Title = "MainPage 2";
  18. Content = new StackLayout
  19. {
  20. Children = {
  21. nextPageButton,
  22. previousPageButton
  23. }
  24. };
  25. }
  26. async void OnNextPageButtonClicked(object sender, EventArgs e)
  27. {
  28. await Navigation.PushAsync(new MainPage3());
  29. }
  30. async void OnPreviousPageButtonClicked(object sender, EventArgs e)
  31. {
  32. await Navigation.PopAsync();
  33. }
  34. }
  35. }
Step 4

In this step, add a new class named MainPage3 and add the following code.

CS code
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Xamarin.Forms;
  7. namespace NavigtionHirechical
  8. {
  9. class MainPage3 : ContentPage
  10. {
  11. public MainPage3()
  12. {
  13. var nextPageButton = new Button { Text = "Next Navigation", VerticalOptions = LayoutOptions.CenterAndExpand };
  14. nextPageButton.Clicked += OnNextPageButtonClicked;
  15. var previousPageButton = new Button { Text = "Previous MainPage2", VerticalOptions = LayoutOptions.CenterAndExpand };
  16. previousPageButton.Clicked += OnPreviousPageButtonClicked;
  17. Title = "Page 2a";
  18. Content = new StackLayout
  19. {
  20. Children = {
  21. nextPageButton,
  22. previousPageButton
  23. }
  24. };
  25. }
  26. async void OnNextPageButtonClicked(object sender, EventArgs e)
  27. {
  28. await Navigation.PushAsync(new NavigationPage1());
  29. }
  30. async void OnPreviousPageButtonClicked(object sender, EventArgs e)
  31. {
  32. await Navigation.PopAsync();
  33. }
  34. }
  35. }
Step 5

Now, add another new class named NavigationPage1. Here is the code for this class.

CS code
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Xamarin.Forms;
  7. namespace NavigtionHirechical
  8. {
  9. class NavigationPage1 : ContentPage
  10. {
  11. public NavigationPage1()
  12. {
  13. var previousPageButton = new Button { Text = "Previous MainPage", VerticalOptions = LayoutOptions.CenterAndExpand };
  14. previousPageButton.Clicked += OnPreviousPageButtonClicked;
  15. var rootPageButton = new Button { Text = "Return to Root MainPage", VerticalOptions = LayoutOptions.CenterAndExpand };
  16. rootPageButton.Clicked += OnRootPageButtonClicked;
  17. var insertPageButton = new Button
  18. {
  19. Text = "Insert Page 2a Before Page 3",
  20. VerticalOptions = LayoutOptions.CenterAndExpand
  21. };
  22. insertPageButton.Clicked += OnInsertPageButtonClicked;
  23. var removePageButton = new Button { Text = "Remove MainPage 2", VerticalOptions = LayoutOptions.CenterAndExpand };
  24. removePageButton.Clicked += OnRemovePageButtonClicked;
  25. Title = "Final Page";
  26. Content = new StackLayout
  27. {
  28. Children = {
  29. previousPageButton,
  30. rootPageButton,
  31. insertPageButton,
  32. removePageButton
  33. }
  34. };
  35. }
  36. async void OnPreviousPageButtonClicked(object sender, EventArgs e)
  37. {
  38. await Navigation.PopAsync();
  39. }
  40. async void OnRootPageButtonClicked(object sender, EventArgs e)
  41. {
  42. await Navigation.PopToRootAsync();
  43. }
  44. void OnInsertPageButtonClicked(object sender, EventArgs e)
  45. {
  46. var page2a = Navigation.NavigationStack.FirstOrDefault(p => p.Title == "Page 2a");
  47. if (page2a == null)
  48. {
  49. Navigation.InsertPageBefore(new MainPage3(), this);
  50. }
  51. }
  52. void OnRemovePageButtonClicked(object sender, EventArgs e)
  53. {
  54. var page2 = Navigation.NavigationStack.FirstOrDefault(p => p.Title == "Page 2");
  55. if (page2 != null)
  56. {
  57. Navigation.RemovePage(page2);
  58. }
  59. }
  60. }
  61. }
Step 6

After the design view creation, go to Solution Explorer >> HierarchicalNavigation(PCL) >> click to open App.xaml.cs. The code is given below.

CS code
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Xamarin.Forms;
  6. namespace NavigtionHirechical
  7. {
  8. public partial class App : Application
  9. {
  10. public App()
  11. {
  12. InitializeComponent();
  13. MainPage = new NavigationPage(new MainPage1());
  14. }
  15. protected override void OnStart()
  16. {
  17. // Handle when your app starts
  18. }
  19. protected override void OnSleep()
  20. {
  21. // Handle when your app sleeps
  22. }
  23. protected override void OnResume()
  24. {
  25. // Handle when your app resumes
  26. }
  27. }
  28. }
Step 7

Click 'F5' or "Build " to 'Run' your application. Running this project, you will have the result like below.

Finally, we have successfully created a Xamarin.Forms Hierarchical Navigation Application.