lunes, 21 de noviembre de 2016

The Xamarin.Forms option

On May 28, 2014, Xamarin introduced Xamarin.Forms, which allows you to write user-interface code that can be compiled for the iOS, Android, and Windows devices.

Xamarin.Forms supports five distinct application platforms:

  • iOS for programs that run on the iPhone, iPad, and iPod Touch.
  • Android for programs that run on Android phones and tablets.
  • The Universal Windows Platform (UWP) for applications that runs under Windows 10 or Windows 10 Mobile.
  • The Windows Runtime API of Windows 8.1.
  • The Windows Runtime API of Windows Phone 8.1.

In this book, “Windows” or “Windows Phone” will generally be used as a generic term to describe all three of the Microsoft platforms.

In the general case, a Xamarin.Forms application in Visual Studio consists of five separate projects for each of these five platforms, with a sixth project containing common code. But the five platform projects in a Xamarin.Forms application are typically quite small—often consisting of just stubs with a little boilerplate startup code. The PCL or SAP contains the bulk of the application, including the user-interface code. The following diagram shows just the iOS, Android, and Universal Windows Platform. The other two Windows platforms are similar to UWP:

The Xamarin.Forms.Core and Xamarin.Forms.Xaml libraries implement the Xamarin.Forms API. Depending on the platform, Xamarin.Forms.Core then makes use of one of the Xamarin.Forms.Plat-form libraries. These libraries are mostly a collection of classes called renderers that transform the Xamarin.Forms user-interface objects into the platform-specific user interface.

The remainder of the diagram is the same as the one shown earlier.

For example, suppose you need the user-interface object discussed earlier that allows the user to toggle a Boolean value. When programming for Xamarin.Forms, this is called a Switch, and a class named Switch is implemented in the Xamarin.Forms.Core library. In the individual renderers for the three platforms, this Switch is mapped to a UISwitch on the iPhone, a Switch on Android, and a ToggleSwitch on Windows Phone.

Xamarin.Forms.Core also contains a class named Slider for displaying a horizontal bar that the user manipulates to choose a numeric value. In the renderers in the platform-specific libraries, this is mapped to a UISlider on the iPhone, a SeekBar on Android, and a Slider on Windows Phone.

This means that when you write a Xamarin.Forms program that has a Switch or a Slider, what’s actually displayed is the corresponding object implemented in each platform.

Here’s a little Xamarin.Forms program containing a Label reading “Hello, Xamarin.Forms!”, a Button saying “Click Me!”, a Switch, and a Slider. The program is running on (from left to right) the iPhone, an Android phone, and a Windows 10 Mobile device:

The iPhone screenshot is of an iPhone 6 simulator running iOS 9.2. The Android phone is an LG Nexus 5 running Android version 6. The Windows 10 Mobile device is a Nokia Lumia 935 running a Windows 10 Technical Preview.

You’ll encounter triple screenshots like this one throughout this book. They’re always in the same order—iPhone, Android, and Windows 10 Mobile—and they’re always running the same program.

As you can see, the Button, Switch, and Slider all have different appearances on the three phones because they are all rendered with the object specific to each platform.

What’s even more interesting is the inclusion in this program of six ToolBarItem objects, three identified as primary items with icons, and three as secondary items without icons. On the iPhone these are rendered with UIBarButtonItem objects as the three icons and three buttons at the top of the page. On the Android, the first three are rendered as items on an ActionBar, also at the top of the page. On Windows 10 Mobile, they’re realized as items on the CommandBar at the page’s bottom.

The Android ActionBar has a vertical ellipsis and the Universal Windows Platform CommandBar has a horizontal ellipsis. Tapping this ellipsis causes the secondary items to be displayed in a manner appropriate to these two platforms:

Xamarin.Forms was originally conceived as a platform-independent API for mobile devices. How-ever, Xamarin.Forms is not limited to phones. Here’s the same program running on an iPad Air 2 simulator:

Most of the programs in this book are fairly simple, and hence designed to look their best on a phone screen in portrait mode. But they will also run in landscape mode and on tablets.

Here’s the UWP project on a Microsoft Surface Pro 3 running Windows 10:

Notice the toolbar at the top of the screen. The ellipsis has already been pressed to reveal the three secondary items.

The other two platforms supported by Xamarin.Forms are Windows 8.1 and Windows Phone 8.1. Here’s the Windows 8.1 program running in a window on the Windows 10 desktop, and the Windows 8.1 program running on the Windows 10 Mobile device:

The Windows 8.1 screen has been left-clicked with the mouse to reveal the toolbar items at the bot-tom. On this screen, the secondary items are at the left, but the program neglectfully forgot to assign them icons. On the Windows Phone 8.1 screen, the ellipsis at the bottom has been pressed.

The various implementations of the toolbar reveals that, in one sense, Xamarin.Forms is an API that virtualizes not only the user-interface elements on each platform, but also the user-interface para-digms.

I hope I have helped in something. Until the next opportunity!



No hay comentarios:

Publicar un comentario en la entrada