Reader small image

You're reading from  Cross-platform UI Development with Xamarin.Forms

Product typeBook
Published inAug 2015
Reading LevelBeginner
Publisher
ISBN-139781784391195
Edition1st Edition
Languages
Tools
Right arrow
Author (1)
Paul Johnson
Paul Johnson
author image
Paul Johnson

Paul Johnson has been writing software since the early 1980s on machines ranging from the ZX81 and servers to his trusty Mac, and has used more languages than he can remember. He is a qualified scuba diver and college lecturer. Paul lives with his wife, kids, and pets, and listens to an inordinate amount of rock and metal on Primordial Radio. This is his third book for Packt.
Read more about Paul Johnson

Right arrow

Chapter 2. Let's Get the Party Started

This chapter is primarily involved with the introduction of the Xamarin Forms library, and its incorporation within your applications. In particular, we will cover the following topics:

  • Setting up for Xamarin Forms

  • How Xamarin Forms work

  • Adding a Windows Phone project to your app

What is Xamarin Forms?


Xamarin Forms is known as a user interface abstraction library. It operates as a Portable Class Library (PCL), sitting on the target platforms and feeding in the UI elements that the application needs. It is as good as it sounds! Application user interfaces can be quickly constructed as a large amount of any other code can be shared.

Note

The Xamarin Forms library also allows you to access the platform hardware through injection with UI customization, through custom renderers. Both of these topics are covered later in the book.

Being an abstraction layer, only those elements with an analogous element on all of the target platforms are covered. For example, at the time of writing, there is not a single checkbox in forms and graphics on tab pages, for Android gives nothing.

This library is also part of a PCL. The PCL itself only supports a subset of the standard .NET library classes, namely those that are supported on all platforms. This leads to quite a few issues, such...

Instantiating Xamarin Forms within an app


We instantiate Xamarin Forms within each supported platform of the app. Xamarin Forms support the following platforms:

  • Android

  • iOS

  • Windows Phone

Android

Typically, an activity to start an activity class looks like this:

public class MyActivity : Activity
{
    protected override void OnCreate(Bundle bundle)
    {
         base.OnCreate(bundle);
         // then whatever you need to do

To start a Xamarin Forms app, this changes as we are not inheriting the Activity but a FormsApplicationActivity, or more precisely:

public class MyActivity : global::Xamarin.Forms.Platform.Android.FormsApplicationActivity
{
    protected override void OnCreate(Bundle bundle)
    {
         base.OnCreate(bundle);
         global::Xamarin.Forms.Forms.Init(this, bundle);
         LoadApplication(new App());
    }
}

iOS

As with Android, we inherit FormsApplicationDelegate in AppDelegate instead of the usual UIApplicationDelegate:

[Register("AppDelegate")]
public partial class AppDelegate...

Dependency injection


Another important aspect of Xamarin Forms is based on something called Inversion of Control (IoC).

Inversion of Control?

Once the app has started, the PCL runs the show; it is in control. However, certain things have to use the device or facilities on the device, and so control is passed to the application (control has been inverted). This can be considered using the following diagram:

https://jonlennartaasenden.files.wordpress.com/2015/01/traditional-vs-di.gif

IoC and Dependency Injection go hand-in-hand. DI (as the name implies) injects information directly into the PCL. There is a rule, though, to do this: the class being called must have a default constructor for the class (normally, the default constructor for any class is not required).

Implementing DI

Let's take the following example for DI. A PCL only contains a subset of the standard libraries; one important omission is anything to do with the filer. When you create a SQLite database though, a file has to be written...

Customizing the UI


While the basic UI elements are available, to do anything interesting (such as customizing buttons), a custom renderer has to be created. These are covered fully in Chapter 6, A View to a Kill, which deals with the user experience, as well as other places within the book.

Gestures, maps, and WebViews


Gestures, maps, and WebViews are very commonly used user-interface facilities. While they are all in Xamarin Forms, we will see that they are not as extensive as if they were native versions.

Gestures

All mobile devices have some form of a sweep system. The iOS is especially rich when it comes to having a multi-touch user interface. As with everything to do with Xamarin.Forms, it only covers the basic type of touch, essentially the only one supported on all platforms.

Note

Code for the following is given in Chapter2/Gestures.

Gestures are not enabled on all types of gadgets (for example, gestures are not enabled for dragging pins on a map). This is because only tap detection is currently supported.

Adding a gesture recognizer

In this example, we'll add a gesture recognizer to a label. Labels usually don't have a click event.

  1. Let's create the label:

    var count = 0;
    
    var label = new Label()
    {
      Text = string.Format("You have clicked me {0} times", count),
      TextColor = Color...

Summary


From the get-go, Xamarin Forms gives you the ability to create an app on the three main mobile platforms without many problems. Most of the standard UI elements are present, and enable the production of feature-rich applications. As we will see in later chapters, using custom renderers as well as being able to access the hardware-specific facilities can extend applications.

In the next chapter, we'll be looking at how we can improve upon the user experience by modifying the user interface to make it more friendly and intuitive.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Cross-platform UI Development with Xamarin.Forms
Published in: Aug 2015Publisher: ISBN-13: 9781784391195
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Paul Johnson

Paul Johnson has been writing software since the early 1980s on machines ranging from the ZX81 and servers to his trusty Mac, and has used more languages than he can remember. He is a qualified scuba diver and college lecturer. Paul lives with his wife, kids, and pets, and listens to an inordinate amount of rock and metal on Primordial Radio. This is his third book for Packt.
Read more about Paul Johnson