Friday, August 23, 2013

Getting Started with Mobility, Part 6: Hybrid Development in C# with Xamarin

In this series of posts, we're looking at how to get started as a mobile developer. In Part 1, we provided an overview of the mobile landscape, and discussed various types of client app development (native, hybrid, mobile web) and supporting back-end services. In Parts 2-5, we examined native app development for iOS, Android, Windows Phone, and Windows 8. Here in Part 6, we'll begin examining hybrid development approaches--starting with Xamarin.

Hybrid Frameworks: Assess the Risks
Hybrid mobile frameworks are a mixed bag: they may make all the difference in getting developers up to speed and productive; but they can also bring consequences. Before adopting any hybrid framework, you should get the answers to some key questions:
  • Does the hybrid framework have a runtime layer or execute in a browser, or does it compile to native code?
  • Does it support all of the mobile platforms you need to target?
  • Does it restrict access to some of the features you need?
  • Does it increase overhead or compromise performance?
  • What is its cost? Is it a one-time cost, or a subscription?
  • What are the limitations imposed by the license terms?
  • What risks come with having a third-party dependency?
  • Is it well-established and trustworthy, or is it new and unproven?
  • Does the producer have a good track record of good relations with mobile platform vendors, and are they able to stay in sync with mobile platform updates in a timely manner?
  • Is good support available, including community forums?

Above all, don't make the mistake of thinking all hybrid solutions are similar. They vary greatly in their approach, mechanics, performance, and reliability.

Xamarin: Mobile Development in C#
Xamarin is a third-party product that allows you to develop for iOS and Android in C# and .NET--very familiar territory to Microsoft developers. How is this magic accomplished? From the Xamarin web site: "Write your app in C# and call any native platform APIs directly from C#. The Xamarin compiler bundles the .NET runtime and outputs a native ARM executable, packaged as an iOS or Android app.". In short, Microsoft developers can develop native apps for iOS and Android while working in a familiar language, C#.


Xamarin is the most expensive of the various mobile development approaches we're considering in this series: the Business Edition developer license costs $1,000 per target platform, per developer, per year (there are also Starter, Indie and Enterprise editions available). It may well be worth the cost, depending on what programming language your developers are familiar with.

The Computer You'll Develop on: A PC or a Mac
Xamarin development for iOS requires a Mac; Android development can be done from a PC or a Mac.

Developer Tools: Xamarin Studio
Xamarin provides an IDE, Xamarin Studio, in both Mac OS X and Windows editions. It's also possible to use Xamarin with Visual Studio.

Xamarin Studio requires and makes use of the Software Development Kits for the mobile platform(s) you are targeting--automatically downloading them during installation if it needs to.

For Android development, you can work self-contained in Xamarin Studio: there's a screen editor, allowing you to see what your layout XML looks like visually. For iOS development, on the other hand, you have to use both Xamarin Studio and Apple's XCode side-by-side, so that you can use Interface Builder.

Xamarin Studio
 
Programming: C#
In Xamarin you program in C#, the favorite language of Microsoft.NET developers. Here's an example of Xamarin C# code for iOS (compare to Objective C):
 
var context = CIContext.FromOptions (new CIContextOptions () {
    UseSoftwareRenderer = true
});
var ciImage = new CIImage (cgImage);
var hueAdjustFilter = new CIHueAdjust {
    InputAngle = 3.0f * Math.PI,
    Image = ciImage,
};

var colorControlsFilter = new CIColorControls {
    InputSaturation = 1.3f,
    InputBrightness = 0.3f,
    Image = hueAdjustFilter.OutputImage
};

ciImage = colorControlsFilter.OutputImage;
context.CreateImage (ciImage, ciImage.Extent);

And, here's an example of Xamarin C# code for Android (normally programmed in Java):

using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;

namespace Android1
{
    [Activity (Label = "Android1", MainLauncher = true)]
    public class MainActivity : Activity
    {
        int count = 1;

        protected override void OnCreate (Bundle bundle)
        {
            base.OnCreate (bundle);

            // Set our view from the "main" layout resource
            SetContentView (Resource.Layout.Main);

            // Get our button from the layout resource,
            // and attach an event to it
            Button button = FindViewById<Button> (Resource.Id.myButton);
           
            button.Click += delegate {
                button.Text = string.Format ("{0} clicks!", count++);
            };
        }
    }
}


Debugging Experience
The debugging experience with Xamarin is similar to that of native iOS and Android development: you can run in the mobile platform's emulator, or on an actual device connected to your computer by USB cable. You're using the same emulators you would use in native development, so they are just as good (or bad, or slow).

In getting Xamarin on my PC to work on actual Android devices, I did need to install a Universal Android Debug Bridge driver.

Android Emulator Selection Dialog
 
Android Emulator

Capabilities
Xamarin doesn't get in the way of using device features. For example, your code can access the Accelerometer and the GPS and the Camera; and can access Contacts and Photos.


AccelerometerPlay Sample
 
CameraAppDemo Sample

Online Resources
The Xamarin web site has a good developer center, whose resources include documentation, guides, recipes, samples, videos, and forums.

Xamarin Developer Center

Key online resources:
Getting Started
Introduction to Mobile Development
Building Cross-Platform Applications
Hello, Android
Hello, iPhone
Android APIs
iOS APIs
Recipes
Sample Code
Videos
Forums

Components Store
Xamarin has a components store; the components offer a broad range of functionality, from charting to syncing with DropBox. Some of the components are free.

Components Gallery

Tutorials
There are a number of good tutorials to be found in the Getting Started area of the Xamarin Developer Center.

1a. Android: Installing Xamarin.Android
1b. iOS: Installing Xamarin.iOS

This tutorial walks you through installation of Xamarin for Android or iOS, and has both Windows PC and Mac editions.

Installing Xamarin on a Mac 

2a. Android: Hello, Android
2b iOS: Hello, iPhone

This tutorial explains how to build a Hello, World application for Android or iOS. It takes you through creating a new project, defining a simple interface and writing simple action code, building the solution, and running it on an emulator.

Creating a New Project in Xamarin Studio

Defining iPhone User Interface in XCode Interface Builder

Hello, iPhone running in iOS Simulator
 
3a. Android: Multi-Screen Applications
3b. iOS: Multi-Screen Applications

This tutorial shows how to create a multi-screen app, with data from the first view passed to the second when a button is pressed.

Android Multi-Screen App Tutorial Running in Emulator



iOS Multi-Screen App Tutorial Running in Emulator

Summary: Xamarin Hybrid Mobile Development in C#
Xamarin puts mobile development in easy reach of Microsoft developers. While it's still necessary to learn the APIs and conventions of each mobile platform, taking Objective C and Java out of the equation is nevertheless a major boost. And, being able to use a common language for your iOS and Android projects makes a good deal of code re-use possible.

Next: Getting Started with Mobility, Part 7: Hybrid Development in JavaScript with Titanium

No comments: