Book Image

Mastering Xamarin UI Development

By : Steven F. Daniel
Book Image

Mastering Xamarin UI Development

By: Steven F. Daniel

Overview of this book

<p>Xamarin is the most powerful cross-platform mobile development framework. If you are interested in creating stunning user interfaces for the iOS and Android mobile platforms using the power of Xamarin and Xamarin.Forms, then this is your ticket.</p> <p>This book will provide you the practical skills required to develop real-world Xamarin applications. You will learn how to implement UI structures and layouts, create customized elements, and write C# scripts to customize layouts. You will create UI layouts from scratch so that you can tweak and customize a given UI layout to suit your needs by using Data Templates.</p> <p>Moving on, you will use third-party libraries – such as the Razor template engine that allows you to create your own HTML5 templates within the Xamarin environment – to build a book library Hybrid solution that uses the SQLite.Net library to store, update, retrieve, and delete information within a SQLite local database. You’ll also implement key data-binding techniques that will make your user interfaces dynamic, and create personalized animations and visual effects within your user interfaces using Custom Renderers and the PlatformEffects API to customize and change the appearance of control elements.</p> <p>At the end of this book, you will test your application UI for robust and consistent behavior and then explore techniques to deploy to different platforms.</p>
Table of Contents (19 chapters)
Mastering Xamarin UI Development
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Customer Feedback
Dedication
Preface

Creating the walk trail content page


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:

  1. Create a new ContentPage called WalkTrailPage as you did in the section entitled Creating the walks main page, located within this chapter.

  2. 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.

Adding the Xamarin.Forms.Maps NuGet package

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.

  1. Right-click on the TrackMyWalks solution and choose the Add Packages... menu option, as shown in the following screenshot:

  2. 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:

  3. Finally, click on the Add Package button to add the Xamarin.Forms.Maps NuGet package to the TrackMyWalks core solution.

  4. Repeat the same process to add the Xamarin.Forms.Maps NuGet package for both the iOS and Android projects that are contained within the TrackMyWalks 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.