Implementing the WalkTrailInfoPage code using C#

Now that we have defined our user interface for our ContentPage using XAML, our next step is to begin creating the underlying C# code within our WalkTrailInfoPage code-behind file, which will populate each of our Bindings with the chosen trail information from the WalksMainPage, which will be passed in as a parameter to this class.

Let's take a look at how we can achieve this by following these steps:

Open the WalkTrailInfoPage.xaml.cs code-behind file, ensuring that it is displayed within the code editor, and enter the following code snippet:

     //
// WalkTrailInfoPage.xaml.cs
// Displays related trail information chosen from the WalksMainPage
//
// Created by Steven F. Daniel on 14/05/2018
// Copyright © 2018 GENIESOFT STUDIOS. All rights reserved.
//
using System;
using Xamarin.Forms;
using TrackMyWalks.Models;

namespace TrackMyWalks.Views
{
public partial class WalkTrailInfoPage : ContentPage
{
public WalkTrailInfoPage()
{
InitializeComponent();

// Update the page title for our Walk Information Page
Title = "Trail Walk Information";

// Set the Binding Context for our ContentPage
this.BindingContext = App.SelectedItem;
}
// Instance method that proceeds to begin a new walk trail
public void BeginTrailWalk_Clicked(object sender, EventArgs e)
{
if (App.SelectedItem == null)
return;

Navigation.PushAsync(new WalkDistancePage());
Navigation.RemovePage(this);
}
}
}

Now, let's start by taking a look at what we covered in the preceding code snippet:

  1. First, we started by including a reference to our TrackMyWalks.Models namespace so that we can access our WalkDataModel class as well as the properties. 
  2. Next, within the WalkTrailInfoPage constructor, we modified the Title property for our ContentPage, and then we set the binding context for our ContentPage using the App.SelectedItem which points to our data-model, containing our walk entry information for the chosen item from the ListView on the WalksMainPage.
  3. Finally, we created the BeginTrailWalk_Clicked event method, which will be called whenever the BeginTrailWalk button is tapped. We checked to ensure that our App.SelectedItem object is valid and then called the WalkDistancePage, using the Navigation.PushAsync method and then calling the RemovePage method of the Navigation class to remove the WalkTrailInfoPage from the navigation stack.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.143.255.36