As mentioned in the previous section, the WalksPage
will essentially serve as the main entry point for our application. We will use our WalkEntries
model to populate some static walks information data, then display this information within a ListView
control using a DataTemplate
. So let's get started by following these steps:
Firstly, create a new folder within the
TrackMyWalks
Portable Class Library project solution calledPages
, as you did in the previous section.Next, from the New File screen, select the Forms section within the left section pane.
Then, select the Forms ContentPage option in the right pane.
Next, enter
WalksPage
for the name of the new class to be created.Finally, click on the New button, as shown in the following screenshot:
Next, ensure that the
WalksPage.cs
file is displayed within the code editor and enter in the following highlighted code sections.
// // WalksPage.cs // TrackMyWalks // // Created by Steven F. Daniel on 04/08/2016. // Copyright © 2016 GENIESOFT STUDIOS. All rights reserved. // using System.Collections.Generic; using Xamarin.Forms; using TrackMyWalks.Models; namespace TrackMyWalks { public class WalksPage : ContentPage { public WalksPage() { var newWalkItem = new ToolbarItem { Text = "Add Walk" }; newWalkItem.Clicked += (sender, e) => { Navigation.PushAsync(new WalkEntryPage()); }; ToolbarItems.Add(newWalkItem); var walkItems = new List<WalkEntries> { new WalkEntries { Title = "10 Mile Brook Trail, Margaret River", Notes = "The 10 Mile Brook Trail starts in the Rotary Park near Old Kate, a preserved steam " + "engine at the northern edge of Margaret River. ", Latitude = -33.9727604, Longitude = 115.0861599, Kilometers = 7.5, Distance = 0, Difficulty= "Medium", ImageUrl = "http://trailswa.com.au/media/cache/media/ images/trails/_mid/FullSizeRender1_600_480_c1.jpg" }, new WalkEntries { Title = "Ancient Empire Walk, Valley of the Giants", Notes = "The Ancient Empire is a 450 metre walk trail that takes you around and through some of " + "the giant tingle trees including the most popular of the gnarled veterans, known as Grandma Tingle.", Latitude = -34.9749188, Longitude = 117.3560796, Kilometers = 450, Distance = 0, Difficulty = "Hard", ImageUrl = "http://trailswa.com.au/media/cache/media/ images/trails/_mid/Ancient_Empire_534_480_c1.jpg" }, }; var itemTemplate = new DataTemplate(typeof(ImageCell)); itemTemplate.SetBinding(TextCell.TextProperty, "Title"); itemTemplate.SetBinding(TextCell.DetailProperty, "Notes"); itemTemplate.SetBinding(ImageCell.ImageSourceProperty, "ImageUrl"); var walksList = new ListView { HasUnevenRows = true, ItemTemplate = itemTemplate, ItemsSource = walkItems, SeparatorColor = Color.FromHex("#ddd"), }; // Set up our event handler walksList.ItemTapped += (object sender, ItemTappedEventArgs e) => { var item = (WalkEntries)e.Item; if (item == null) return; Navigation.PushAsync(new WalkTrailPage(item)); item = null; }; Content = walksList; } } }
In the preceding code snippet, we began by declaring our newWalkItem
variable that instantiates from the ToolbarItem
class which will be used to attach a new Add Walk button to the main toolbar of the base ContentPage.ToolbarItems
collection to provide a way for users to add new walk trail information within the app.
Next, we create an event for our new WalkItem
using the Clicked
event of the ToolbarItem
class, which will be used to navigate to the new WalksEntryPage
.
In our next step, we declare a new variable walkItems
that is a collection of list items to store each of our walk entries within our model and then use the DataTemplate
class to describe how we want our model data to be displayed within each of the rows declared within the ListView
.
Finally, we set up an event handler for our ListView
that will be used to move to the WalksTrailPage
to display information about the item selected.