Chapter 1. Creating the TrackMyWalks Native App

Since Xamarin made its appearance several years ago, developers have been delighted with being able to create native mobile applications that target non-Microsoft platforms, and with having the option of developing apps using either C# or F# programming languages, which enables developers to distribute their app ideas on iOS and Android platforms.

As you progress through this book, you will learn how to apply best practice principles when developing cross-platform mobile applications and design patterns using the Xamarin.Forms platform, which allows developers to build cross-platform user interface layouts that can be shared across Android, iOS, and Windows Phone mobile platforms.

Since each of these apps can be written using a single programming language, it makes sense to write a single codebase that would compile and build separate apps for each of these different platforms.

This chapter will begin by setting up a basic structure of an app built using Xamarin.Forms, which will be the foundation for the subsequent chapters, where we will continually build upon this, applying new concepts. In this chapter, you will see how to create an initial cross-platform native app using Xamarin.Forms and how to go about adding new, and updating existing, packages within your solution.

You'll learn how to create C# classes that will act as the model for our app, as well as creating content pages that will form the user interface. To end the chapter, you will learn about the differences between developing apps using Xamarin Studio and/or Microsoft Visual Studio.

This chapter will cover the following points:

  • Creating the Xamarin.Forms TrackMyWalks mobile app solution
  • Updating the TrackMyWalks solution packages using the NuGet package manager
  • Creating the TrackMyWalks data model
  • Creating the ContentPages for the TrackMyWalks solution
  • Understanding the differences between Xamarin Studio and Visual Studio

Creating the TrackMyWalks solution

In this section, we will take a look at how we can go about creating a new Xamarin.Forms solution for the first time. We will begin by developing the basic structure for our application, as well as by adding the necessary entity models and designing the user interface files.

Before we can proceed, we need to create our TrackMyWalks project. It is very simple to create this using Xamarin Studio. Simply follow the steps listed below:

  1. Launch the Xamarin Studio application. You will be presented with the following screen:

    Creating the TrackMyWalks solution

  2. Next, click on the New Solution... button, or alternatively choose the File | New | Solution... or simply press Shift + Command + N.
  3. Next, choose the Forms App option which is located under the Multiplatform | App section. Ensure you have selected C# as the programming language to use:

    Creating the TrackMyWalks solution

  4. Next, enter TrackMyWalks to use as the name for your app in the App Name field.
  5. Then, specify a name for the Organization Identifier field.
  6. Next, ensure that both the Android and iOS checkboxes have been selected in the Target Platforms fields.
  7. Then, ensure that the Use Portable Class Library option has been selected in the Shared Code section, as shown in the following screenshot.

    Tip

    The difference between using a Portable Class Library versus a Shared Library is essentially that a Portable Class Library enables developers to write code once, so that it can be used within different platform projects such as Websites, Android, and iOS. A Shared Library enables developers to copy all the files within the library project to all the projects that are contained within the solution during compilation for the various platforms that will use it.

    Creating the TrackMyWalks solution

    Note

    The Organization Identifier option for your app needs to be unique. Xamarin Studio recommends that you use the reverse domain style (for example, com.domainName.appName).

  8. Next, ensure that the Use XAML for user interface files option has not been selected.
  9. Then, click on the Next button to proceed to the next step in the wizard.

    Creating the TrackMyWalks solution

  10. Next, ensure that the Create a project directory within the solution directory. checkbox has been selected.
  11. Then, click on the Create button to save your project to the specified location.

Once your project has been created, you will be presented with the Xamarin development environment along with several project files that the template has created for you, as shown in the following screenshot:

Creating the TrackMyWalks solution

As you can see from the preceding screenshot, the TrackMyWalks solution has been divided into three main areas. The following table provides a brief description of what each area is used for:

Platform specific project

Description

TrackMyWalks

This is the Portable Class Library (PCL) project that will be responsible for acting as the main architectural layer for the TrackMyWalks solution.

This project contains all of the business logic, data objects, Xamarin.FormsPages, views, and other non-platform specific code.

Any code that you create within this project can be shared across multiple platform-specific projects.

TrackMyWalks.Droid

This project is an Android specific project that contains all of the code and assets required to build and deploy the Android app contained within the solution.

By default, this project contains a reference to the TrackMyWalks Portable Class Library.

TrackMyWalks.iOS

This project is an iOS specific project that contains all of the code and assets required to build and deploy the iOS app contained within the solution.

By default, this project contains a reference to the TrackMyWalks Portable Class Library.

One thing you will notice is that our solution contains a file called TrackMyWalks.cs which is part of the TrackMyWalks Portable Class Library. The TrackMyWalks.cs file contains a class named App that inherits from the Xamarin.Forms.Application class hierarchy, as can be seen in the following code snippet:
    // 
    //  TrackMyWalks.cs 
    //  TrackMyWalks 
    // 
    //  Created by Steven F. Daniel on 04/08/2016. 
    //  Copyright © 2016 GENIESOFT STUDIOS. All rights reserved. 
    // 
    using Xamarin.Forms; 
 
    namespace TrackMyWalks 
    { 
        public class App : Application 
       { 
            public App() 
            {             
    // The root page of your application
                var content = new ContentPage 
                { 
                    Title = "TrackMyWalks",
                      Content = new StackLayout { 
                          VerticalOptions = LayoutOptions.Center, 
                            Children = { new Label { 
                                HorizontalTextAlignment = 
                                TextAlignment.Center, 
                                Text = "Welcome to Xamarin Forms!" 
                            } 
                        } 
                    } 
                }; 
 
                MainPage = new NavigationPage(content); 
            } 
 
            protected override void OnStart() 
           { 
                // Handle when your app starts 
            } 
            protected override void OnSleep() 
            { 
                // Handle when your app sleeps 
            } 
            protected override void OnResume() 
            { 
                // Handle when your app resumes 
            } 
         } 
       } 

The App constructor method sets up the MainPage property to a new instance of the ContentPage that will simply display some default text as created by the project wizard. Throughout this chapter, we will be building the initial user interface page views and then modifying the MainPage property for our App class, contained within the TrackMyWalks.cs file.

Updating the TrackMyWalks solution packages

In this section, we will take a look at how to update the Xamarin.Forms packages contained within our TrackMyWalks solution. Basically, you will notice that each project contained within our solution contains a Packages folder.

The Xamarin.Forms package is essentially a NuGet package that gets automatically included in our solution whenever we specify that we want to create a Xamarin.FormsApp project template.

From time to time, you will notice that Xamarin will notify you whenever a package is out of date and needs to be updated to ensure that you are running the latest version.

Note

A NuGet package, is essentially the package manager for the Microsoft Development Platform that contains the client tools that provide the capability for producing and consuming .NET packages.

Let's take a look at how to go about updating the NuGet packages within our TrackMyWalks solution to ensure that we are running the latest Xamarin.Forms packages. Perform the following steps:

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

    Updating the TrackMyWalks solution packages

    Once you have selected this option, Xamarin Studio will proceed to update each package that is contained within the TrackMyWalks solution for each of the platform-specific projects, and will display a progress indicator similar to the one shown in the following screenshot:

    Updating the TrackMyWalks solution packages

    During the package update process, some packages that are contained as part of each platform-specific project require you to accept their license terms prior to installing, which is shown in the following screenshot:

    Updating the TrackMyWalks solution packages

  2. Click on the Accept button to accept the license terms and conditions for the packages displayed within the dialog box and to install the packages, as shown in the preceding screenshot.

Now that you have successfully updated the Xamarin.Forms packages within your solution, we can now proceed with building the user interface files for the TrackMyWalks solution.

..................Content has been hidden....................

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