Developing Mobile Applications With Xamarin

As a software engineer, I’m constantly thinking about code reuse. How can I write code once, but utilize it in other places when needed?

Over the years, I’ve used many different methods to accomplish code reuse, including Web User Controls in ASP.NET Forms applications, Partial Views in ASP.NET MVC applications, shared code libraries (dll’s), and web services. All of these instruments have something in common: they’ve all been for the same platform, i.e. Microsoft Windows and related technologies such as IIS (web server).

Developing for mobile devices complicates things. There are two primary platforms used by the majority of devices today, Google’s Android and Apple’s iOS. These platforms are very different in that they use completely different programming languages, and neither uses C#, my programming language of choice. This makes sharing code between Android and iOS versions of the same application a little more difficult.

Enter Xamarin. Xamarin allows me to write most, if not all of my code in C#, for both Android and iOS devices. There are three primary Xamarin libraries that are used to do this: Xamarin.Android, Xamarin.iOS and Xamarin.Forms. Each of these libraries serve a specific purpose, and choosing between them requires knowledge of each.

Xamarin.Android

The Xamarin.Android library, as you can infer from the name, is for developing applications for the Android platform. Xamarin provides a group of wrappers for all of the Android Java libraries, all of the Android API and most, but not all of the Mono.NET library. Third-party Java libraries such as ArcGIS and PayPal libraries can also be incorporated into Xamarin.Android applications. Xamarin.Android provides the tools required to create and run native Android applications.

Xamarin.iOS

Like Xamarin.Android, Xamarin.iOS is a platform-specific library used for code directly targeting macOS and iOS devices. Visual Studio includes templates to create a variety of applications targeting these devices, including iPhones, Macs, Apple TV and Apple Watch. Xamarin.iOS provides C# wrappers around the Apple SDK and APIs.

Xamarin.Forms

Xamarin.Forms provides a different approach to mobile application development. In Xamarin.Android and Xamarin.iOS, the User Interface code is all located in those platform-specific projects. With Xamarin.Forms, developers can create the User Interface in a shared project and use XAML to create the views and pages. When the compiler builds the application, it converts the shared code into a native application as best it can. There are, however, a few gotchas due to differences in each platform, but luckily, developers can overcome these issues by creating things like custom renderers, using Inversion of Control containers, service locators, abstractions, etc. to safely notify the shared code of platform-specific dependencies.

Tying it All Together

Each mobile application project is different, but most share enough commonalities to make Xamarin a worthy development paradigm. Combined with Microsoft’s Visual Studio and Apple’s Xcode, it adds a powerful set of capabilities to any developer’s toolbox.

Disclaimer: This article is in no way a sales advertisement for any of the products discussed. While I personally develop using Xamarin, I do not work for Xamarin, Google, Microsoft or Apple. I do, however, provide professional development and consulting services using their products.

Leave a Reply

Your email address will not be published. Required fields are marked *