Main image of article Building Apps with Xamarin Forms
shutterstock_401334931 Xamarin is both a company and a cross-platform development system for creating mobile apps in C#. Co-founded by Miguel de Icaza, a developer who founded the Linux GNOME project as well as Mono (the .NET framework-compatible toolset for Linux), Xamarin offers tools such as Xamarin Studio (an IDE for Windows and OS/X) and Visual Studio plugins; using those tools, developers can create apps for iOS, Android, and Windows Phone. (Visual Studio on a Windows PC can talk to any networked Mac used to build and run iOS emulators for iPhone and iPad; if the device is plugged into the Mac, it can be debugged from within Visual Studio running on the PC. Apple has relaxed the requirement that you need to be a registered iOS developer to run your software on your device, although you must still distribute apps via the App Store.) Since Microsoft bought Xamarin in May 2016, the community version of Visual Studio 2015 has come with Xamarin software included. (There’s an irony in Microsoft buying Xamarin at a time when Windows Phones have never been less popular.) If you’re exploring what this software can do for you, the main thing to consider is how you can build apps quickly using Xamarin.Forms:

Development

Developing an app for one platform, then repeating the whole process on another, is slow and inefficient. Xamarin.forms, by contrast, allows developers to create a single app that works across multiple platforms. The core idea is that you split your code into two parts: portable and platform-specific. There are two ways to do this: create Shared Projects, or create Portable Class Libraries (PCL). Shared Projects contain code common to all projects. You can use compiler directives to restrict code to the specific platform of your choice, whether Android, iOS, or Windows:
#if __IOS__

// iOS-specific code

#endif

 

#if __ANDROID__

// Android-specific code

#if __ANDROID_11__

// code that should only run on Android 3.0 Honeycomb or newer

#endif

#endif

While Shared Projects (SP) makes it easier to reuse code, especially across multiple projects, Portable Class Libraries (PCL) does things differently. The PCL targets a combination of platforms of your choice, including .Net Framework, Windows Stores Apps, Silverlight, Windows Phone and Xamarin. One difference between SP and PCL is that you can't use the specific platform compiler directives with PCL. Another big one: with SP, refactoring doesn't work with inactive code such as Android code inside a #if IOS ), whereas in PCL code there's no such problem. With PCL, you can test without worrying about which platform applies to the code, which makes testing simpler. Xamarin provides visual designers for iOS and Android that let you design the GUI (Visual Studio and Blend lets you design the Windows Phone GUI). These designers ultimately generate XML—with Windows Phone, it's XAML; iOS generates Storyboards and Android designer produces AXML.

Down in the GUI

With Xamarin.Forms, the controls are native to the target platform; as a result, a Xamarin Application will look native. You structure your GUI using pages (which are similar to view controllers in iOS and activities in Android), layouts (specialized views such as stacks, grid, and scrolling views), views (which correspond to controls) and cells (used in table or list controls). (You can also check out the Android GUI designer in Google’s Android Studio, which in my opinion is slightly better than the Xamarin one.) Although Xamarin.Forms was quite rough around the edges in its early days, it’s now very usable. While setting up Android emulators can be quite a pain, that’s generally true for any Android development. If you want to learn more about the platform for free, edX offers a short two-week course entitled: “Introduction to Xamarin.Forms.” For those interested in learning how to develop cross-platform apps, the platform may be worth your time.