Introduction

This article demonstrates how to navigate to modal pages. A modal page can be any of the page types (eg.-content page) supported by Xamarin.Forms. To display a modal page, the application will push it onto the navigation stack where it becomes an active page.
Let's start!
Step 1

Click File >> New >> Project >>Visual C# >> Cross-Platform(Xamarin.Forms or Native) >> give project a name and location. Then, click OK.
Step 2

Now, click open Mainpage.xaml and insert the below-given code. For that, go to Solution Explorer >> Modal page (PCL) >> Mainpage.xaml.

Next, insert the toolbar items of ListView to display page names and its clicked events to navigation between the pages.
  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  4. xmlns:local="clr-namespace:ModalpageApp"
  5. x:Class="ModalpageApp.MainPage"
  6. Title="MainPage">
  7. <ContentPage.Content>
  8. <StackLayout >
  9. <ListView x:Name="Listview" ItemSelected="Listview_ItemSelected"/>
  10. </StackLayout>
  11. </ContentPage.Content>
  12. </ContentPage>
Step 3

Then, open Solution Explorer >>Modalpages(PCL) >>Mainpage.xaml.cs page and double click to open the design view. Here is the code for this page.
  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 ModalpageApp
  8. {
  9. public partial class MainPage : ContentPage
  10. {
  11. List<Contacts> contact;
  12. public MainPage()
  13. {
  14. InitializeComponent();
  15. SetUpdata();
  16. Listview.ItemsSource = contact;
  17. }
  18. async void Listview_ItemSelected(object sender, SelectedItemChangedEventArgs e)
  19. {
  20. if (Listview.ItemsSource!=null)
  21. {
  22. var detailpage = new DetailspageCS();
  23. detailpage.BindingContext = e.SelectedItem as Contacts;
  24. Listview.SelectedItem = null;
  25. await Navigation.PushAsync(detailpage);
  26. }
  27. }
  28. void SetUpdata()
  29. {
  30. contact = new List<Contacts>();
  31. contact.Add(new Contacts
  32. {
  33. Name = "Logesh",
  34. Age=20,
  35. Occupation="Farmer",
  36. Country="India"
  37. });
  38. contact.Add(new Contacts
  39. {
  40. Name = "Hari",
  41. Age = 20,
  42. Occupation = "Software Engineer",
  43. Country = "India"
  44. });
  45. contact.Add(new Contacts
  46. {
  47. Name = "Mani",
  48. Age = 20,
  49. Occupation = "Farmer",
  50. Country = "India"
  51. });
  52. contact.Add(new Contacts
  53. {
  54. Name = "Guna Seker",
  55. Age = 20,
  56. Occupation = "Farmer",
  57. Country = "India"
  58. });
  59. contact.Add(new Contacts
  60. {
  61. Name = "Palani",
  62. Age = 20,
  63. Occupation = "Farmer",
  64. Country = "India"
  65. });
  66. contact.Add(new Contacts
  67. {
  68. Name = "Laxman",
  69. Age = 30,
  70. Occupation = "Driver",
  71. Country = "India"
  72. });
  73. }
  74. }
  75. }
Step 4

Now, create a class named Contacts.cs. For that, go to Solution Explorer>>Modalpage(PCL)>>right click and select Add>> New Item. In this popup window, select CrossPlatform>>class. Give a name such as Contacts.cs.

After double clicking to open it, insert the code given-below.
  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 ModalpageApp
  8. {
  9. public class Contacts
  10. {
  11. public string Name { get; set; }
  12. public int Age { get; set; }
  13. public string Country { get; set; }
  14. public string Occupation { get; set; }
  15. public override string ToString()
  16. {
  17. return Name;
  18. }
  19. }
  20. }
Step 5

Similarly, create another class named Masterdetails.cs. This page is used when the user clicks on listview items after navigating t a new page to display details.
  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 ModalpageApp
  8. {
  9. public class DetailspageCS : ContentPage
  10. {
  11. public DetailspageCS()
  12. {
  13. var nameLabel = new Label
  14. {
  15. FontSize = Device.GetNamedSize(NamedSize.Medium, typeof(Label)),
  16. FontAttributes = FontAttributes.Bold
  17. };
  18. nameLabel.SetBinding(Label.TextProperty, "Name");
  19. var ageLabel = new Label
  20. {
  21. FontSize = Device.GetNamedSize(NamedSize.Medium, typeof(Label)),
  22. FontAttributes = FontAttributes.Bold
  23. };
  24. ageLabel.SetBinding(Label.TextProperty, "Age");
  25. var occupationLabel = new Label
  26. {
  27. FontSize = Device.GetNamedSize(NamedSize.Medium, typeof(Label)),
  28. FontAttributes = FontAttributes.Bold
  29. };
  30. occupationLabel.SetBinding(Label.TextProperty, "Occupation");
  31. var countryLabel = new Label
  32. {
  33. FontSize = Device.GetNamedSize(NamedSize.Medium, typeof(Label)),
  34. FontAttributes = FontAttributes.Bold
  35. };
  36. countryLabel.SetBinding(Label.TextProperty, "Country");
  37. var dismissButton = new Button { Text = "Dismiss" };
  38. dismissButton.Clicked += OnDismissButtonClicked;
  39. Padding = new Thickness(0, Device.OnPlatform(20, 0, 0), 0, 0);
  40. Content = new StackLayout
  41. {
  42. HorizontalOptions = LayoutOptions.Center,
  43. VerticalOptions = LayoutOptions.Center,
  44. Children = {
  45. new StackLayout {
  46. Orientation = StackOrientation.Horizontal,
  47. Children = {
  48. new Label {
  49. Text = "Name:",
  50. FontSize = Device.GetNamedSize (NamedSize.Medium, typeof(Label)),
  51. HorizontalOptions = LayoutOptions.FillAndExpand
  52. },
  53. nameLabel
  54. }
  55. },
  56. new StackLayout {
  57. Orientation = StackOrientation.Horizontal,
  58. Children = {
  59. new Label {
  60. Text = "Age:",
  61. FontSize = Device.GetNamedSize (NamedSize.Medium, typeof(Label)),
  62. HorizontalOptions = LayoutOptions.FillAndExpand
  63. },
  64. ageLabel
  65. }
  66. },
  67. new StackLayout {
  68. Orientation = StackOrientation.Horizontal,
  69. Children = {
  70. new Label {
  71. Text = "Occupation:",
  72. FontSize = Device.GetNamedSize (NamedSize.Medium, typeof(Label)),
  73. HorizontalOptions = LayoutOptions.FillAndExpand
  74. },
  75. occupationLabel
  76. }
  77. },
  78. new StackLayout {
  79. Orientation = StackOrientation.Horizontal,
  80. Children = {
  81. new Label {
  82. Text = "Country:",
  83. FontSize = Device.GetNamedSize (NamedSize.Medium, typeof(Label)),
  84. HorizontalOptions = LayoutOptions.FillAndExpand
  85. },
  86. countryLabel
  87. }
  88. },
  89. dismissButton
  90. }
  91. };
  92. }
  93. async void OnDismissButtonClicked(object sender, EventArgs args)
  94. {
  95. await Navigation.PopModalAsync();
  96. }
  97. }
  98. }
Step 6

Then, open App.xaml.cs page to declare that the Main Page of application is Mainpage.xaml and it's navigation pages. For that, go to Solution Explorer >> Modal page (PCL) >> click open App.xaml.cs and put the code given below.
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Xamarin.Forms;
  6. namespace ModalpageApp
  7. {
  8. public partial class App : Application
  9. {
  10. public App()
  11. {
  12. InitializeComponent();
  13. MainPage = new NavigationPage( new MainPage());
  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

Press F5 or "Build and Run" your application. The result is shown below like this.
Finally, we have successfully created a Xamarin.Forms Modalpage application.