In this section, we will begin building the user interface for our WalksTrailPage
. This page is called when the user clicks on an entry within the ListView
on our TrackMyWalks
main content page and will be used to display information associated with the chosen trail:
Create a new
ContentPage
calledWalkTrailPage
as you did in the section entitled Creating the walks main page, located within this chapter.Next, ensure that the
WalkTrailPage.cs
file is displayed within the code editor and enter the following highlighted code sections:
// // WalkTrailPage.cs // TrackMyWalks // // Created by Steven F. Daniel on 04/08/2016. // Copyright © 2016 GENIESOFT STUDIOS. All rights reserved. // using Xamarin.Forms; using TrackMyWalks.Models; namespace TrackMyWalks { public class WalkTrailPage : ContentPage { public WalkTrailPage(WalkEntries walkItem) { Title = "Walks Trail"; var beginTrailWalk = new Button { BackgroundColor = Color.FromHex("#008080"), TextColor = Color.White, Text = "Begin this Trail" }; // Set up our event handler beginTrailWalk.Clicked += (sender, e) => { if (walkItem == null) return; Navigation.PushAsync(new DistanceTravelledPage (walkItem)); Navigation.RemovePage(this); walkItem = null; }; var walkTrailImage = new Image() { Aspect = Aspect.AspectFill, Source = walkItem.ImageUrl }; var trailNameLabel = new Label() { FontSize = 28, FontAttributes = FontAttributes.Bold, TextColor = Color.Black, Text = walkItem.Title }; var trailKilometersLabel = new Label() { FontAttributes = FontAttributes.Bold, FontSize = 12, TextColor = Color.Black, Text = $"Length: { walkItem.Kilometers } km" }; var trailDifficultyLabel = new Label() { FontAttributes = FontAttributes.Bold, FontSize = 12, TextColor = Color.Black, Text = $"Difficulty: { walkItem.Difficulty } " }; var trailFullDescription = new Label() { FontSize = 11, TextColor = Color.Black, Text = $"{ walkItem.Notes }", HorizontalOptions = LayoutOptions.FillAndExpand }; this.Content = new ScrollView { Padding = 10, Content = new StackLayout { Orientation = StackOrientation.Vertical, HorizontalOptions = LayoutOptions.FillAndExpand, Children = { walkTrailImage, trailNameLabel, trailKilometersLabel, trailDifficultyLabel, trailFullDescription, beginTrailWalk } } }; } } }
In the preceding code snippet, we began by importing our TrackMyWalks.Models
class as we will be using this to extract the information passed in from our WalksPage
.
Next, we declare our beginTrailWalk
variable that inherits from the Button
class; then we set up the Clicked
event of the Button
class, which will be used to navigate to the DistanceTravelledPage
content page when clicked to display information about our trail after removing our walks trail content page from the NavigationPage
hierarchy.
In the next step, we declare an image variable walkTrailImage
and set the Source
property of the image to be the image of the selected walkItem
from the ListView
. We then declare and initialize a number of label objects that will contain the walkItem
information that has been passed from the WalksPage
content page ListView
control and displayed.
Next, we define a ScrollView
control that is part of the Xamarin.Forms.Core
base class, then add each of our form Image
and Label
fields to the StackLayout
control. The ScrollView
control is a fantastic control that allows our ContentPage
to scroll its contents should the information be too big to fit within the actual device's screen real estate.
In this section, we will need to add the Xamarin.Forms.Maps
NuGet package to our core project, as well as for each of the platform-specific projects for both iOS and Android platforms. This package is required in order to use the Xamarin.FormsMap
control in the DistanceTravelled
content page that we will be building in the next section.
Right-click on the
TrackMyWalks
solution and choose the Add Packages... menu option, as shown in the following screenshot:This will display the Add Packages dialog. Enter in
maps
within the Search dialog and then select and click the Xamarin.Forms.Maps option within the list, as shown in the following screenshot:Finally, click on the Add Package button to add the
Xamarin.Forms.Maps
NuGet package to theTrackMyWalks
core solution.Repeat the same process to add the
Xamarin.Forms.Maps
NuGet package for both the iOS and Android projects that are contained within theTrackMyWalks
solution.
Now that you have added the NuGet
Package for the Xamarin.Forms Map
, we can begin to utilize this control within the DistanceTravelled
content page that we will be covering in the next section.