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:
TrackMyWalks
mobile app solutionTrackMyWalks
solution packages using the NuGet
package managerTrackMyWalks
data modelContentPages
for the TrackMyWalks
solutionIn 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:
TrackMyWalks
to use as the name for your app in the App Name field.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.
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:
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 |
|
This is the Portable Class Library (PCL) project that will be responsible for acting as the main architectural layer for the This project contains all of the business logic, data objects, Any code that you create within this project can be shared across multiple platform-specific projects. |
|
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 |
|
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.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.
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.
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:
TrackMyWalks
solution and choose the Update NuGet Packages menu option, as shown in the following screenshot:
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:
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:
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.
3.14.144.229