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 walks main page


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:

  1. Firstly, create a new folder within the TrackMyWalks Portable Class Library project solution called Pages, as you did in the previous section.

  2. Next, from the New File screen, select the Forms section within the left section pane.

  3. Then, select the Forms ContentPage option in the right pane.

  4. Next, enter WalksPage for the name of the new class to be created.

  5. Finally, click on the New button, as shown in the following screenshot:

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