This implementation is a “basic-item” of our own apps. Now, I’m going to show you the code.

UWP

  1. Create a new solution in UWP.



  2. We’ll have the environment, given below-



  3. Add a second page. Right click on the name of the project -> add new item -> blank page (name it SecondPage).

  4. Now, we are ready to write the code. Double click on MainPage.xaml.



  5. Now, we are going to replace the auto generated code. Write the code, given below-
    1. <Page x:Class="Booster1.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:Booster1" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d">
    2. <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    3. <TextBlock x:Name="FirstPage" Text="Main Page" HorizontalAlignment="Left" VerticalAlignment="Top" Height="60" Width="250" FontSize="40" />
    4. <Button x:Name="button" Content="Navigation Test Button" HorizontalAlignment="Center" VerticalAlignment="Center" Height="95" Width="187" Click="button_Click" />
    5. </Grid>
    6. </Page>
    The output is given below-



  6. Now, we’ll write the customized code for the second page. Double click on SecondPage.xaml. Delete the auto generated code and write the code, given below-
    1. <Page x:Class="Booster1.SecondPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:Booster1" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d">
    2. <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    3. <TextBlock x:Name="Main Page" Text="Hi. I'm the second page" HorizontalAlignment="Center" VerticalAlignment="Center" Height="60" Width="300" FontSize="28" />
    4. </Grid>
    5. </Page>
    The output is given below-



  7. Now, we’ll write the code at the backend to manage the logic. Open MainPage.xaml.cs by splitting the MainPage.xaml node and double click on the name of file.

  8. Delete the code and write the following (bold blue for the most important line)-
    1. using System;
    2. using System.Collections.Generic;
    3. using System.IO;
    4. using System.Linq;
    5. using System.Runtime.InteropServices.WindowsRuntime;
    6. using Windows.Foundation;
    7. using Windows.Foundation.Collections;
    8. using Windows.UI.Xaml;
    9. using Windows.UI.Xaml.Controls;
    10. using Windows.UI.Xaml.Controls.Primitives;
    11. using Windows.UI.Xaml.Data;
    12. using Windows.UI.Xaml.Input;
    13. using Windows.UI.Xaml.Media;
    14. using Windows.UI.Xaml.Navigation;
    15. // The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409
    16. namespace Booster1 {
    17. /// <summary>
    18. /// An empty page that can be used on its own or navigated to within a Frame.
    19. /// </summary>
    20. public sealed partial class MainPage: Page {
    21. public MainPage() {
    22. this.InitializeComponent();
    23. }
    24. private void button_Click(object sender, RoutedEventArgs e) {
    25. this.Frame.Navigate(typeof(SecondPage));
    26. }
    27. }
    28. }
  9. Now, if we worked well, we’ll have, after the deployment (CTRL+5), the following outputs-

    Windows 10 Desktop simulator





    Windows 10 Mobile

Xamarin
  1. Create a new solution in Xamarin.Forms Portable (PCL).



  2. We’ll have the following environment-



  3. Add the Main Page,

    Right click on the name of the project portable -> add new item -> Cross Platform -> Code -> Forms ContentPage (name it MainPage).



  4. Add the second page.

    Right click on the name of the project portable -> add new item -> Cross Platform -> Code -> Forms ContentPage (name it SecondPage).



  5. Now, we are ready to write the code. First of all, we’ll rewrite the App.cs code with the following (blue bold for the most important line)-
    1. using System;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4. using System.Text;
    5. using Xamarin.Forms;
    6. namespace Booster2 {
    7. public class App: Application {
    8. public App() {
    9. // The root page of your application
    10. MainPage = new NavigationPage(new MainPage()); {};
    11. }
    12. protected override void OnStart() {
    13. // Handle when your app starts
    14. }
    15. protected override void OnSleep() {
    16. // Handle when your app sleeps
    17. }
    18. protected override void OnResume() {
    19. // Handle when your app resumes
    20. }
    21. }
    22. }
  6. Now, we are going to write the code at the backend for both Main and Second Page (blue bold for the most important line)-

    • Main Page
      1. using System;
      2. using System.Collections.Generic;
      3. using System.Linq;
      4. using System.Reflection.Emit;
      5. using System.Text;
      6. using Xamarin.Forms;
      7. namespace Booster2
      8. {
      9. public class MainPage : ContentPage
      10. {
      11. public MainPage()
      12. {
      13. Title = "Main Page";
      14. Button changePage = new Button
      15. {
      16. Text = "Change Page Test",
      17. HorizontalOptions = LayoutOptions.Center,
      18. VerticalOptions = LayoutOptions.CenterAndExpand
      19. };
      20. changePage.Clicked += async (sender, args) =>
      21. {
      22. await Navigation.PushAsync(new SecondPage());
      23. };
      24. Content = new StackLayout
      25. {
      26. Children =
      27. {
      28. changePage
      29. }
      30. };
      31. }
      32. }
      33. }
    • Second Page
      1. using System;
      2. using System.Collections.Generic;
      3. using System.Linq;
      4. using System.Reflection.Emit;
      5. using System.Text;
      6. using Xamarin.Forms;
      7. namespace Booster2
      8. {
      9. public class SecondPage : ContentPage
      10. {
      11. public SecondPage()
      12. {
      13. Content = new StackLayout
      14. {
      15. Children = {
      16. new Label { Text = "Hi. I'm the Second Page" }
      17. }
      18. };
      19. }
      20. }
      21. }
  7. Now, if we worked well, we’ll have, after the deployment (CTRL+5), the outputs are given below-

    Android



    Windows 10 Desktop Emulator



    Windows 10 Mobile

Conclusions

The methods to implement the navigation are different.
  • UWP
    1. private void button_Click(object sender, RoutedEventArgs e) {
    2. this.Frame.Navigate(typeof(SecondPage));
    3. }
  • Xamarin

    MainPage Logic
    1. changePage.Clicked += async(sender, args) => {
    2. await Navigation.PushAsync(new SecondPage());
    3. };
    The async method does not run necessarily. In this case, it is a second thread.

    Title = "Main Page";

    This property allows us to display the name of the app on the top of the screen with the app icon (on Android).

    App.cs Logic
    1. MainPage = new NavigationPage(new MainPage());
    2. {
    3. };
    The method, given above, specifies that the ContentPage named MainPage is the root page.