How to develop cross-platform apps more easily with Xamarin.Essentials

The combination of Xamarin.Essentials and Xamarin Forms should finally give Microsoft the end-to-end cross platform development story it’s been seeking for PCs and mobile devices

The Xamarin acquisition was one of Microsoft’s smartest deals. It quickly gave it access to tools that let developers use familiar tools and technologies to build cross-platform applications. Now built into every version of Visual Studio, and providing the basis for its MacOS Visual Studio release, Xamarin has become a key element of Microsoft’s development tools.

Until recently—even with Xamarin—building cross-platform applications wasn’t easy. For all that the core development tools handle working with iOS and Android from .Net, using it to build apps meant having significant amounts of device-specific code to handle both native UX and deep platform integration. Although you could keep your core code across device-specific projects, building and testing the full application required domain knowledge and specialized skills. The result was code that, although a little cheaper than using native tools for each platform, really wasn’t as cheap to build as it could have been.

Cross-platform UI and device integration with Xamarin

Microsoft’s platform developers have been working to get around this financial and personnel bottleneck, first with Xamarin Formsand now with the beta release of Xamarin.Essentials.

Xamarin Forms takes care of most of the user interface side of the problem, providing common controls that are rendered appropriately on each supported platform. You can write code, lay out controls and then build versions for all your target devices. While it’s a lowest common denominator approach, it offers controls that fill many of the enterprise application development requirements. You might not want to ship a Xamarin Forms app to users, but it’s more than adequate for in-house apps, such as for sales and for field service.

How then to simplify the other side of your code, handling its interfaces with the underlying platform APIs? Until now you needed to write native code or use custom plug-ins. To get the most from Xamarin, you want the same API calls to work with notifications, with the file system, with the camera, and with location services, no matter what OS you’re targeting. That’s where Xamarin.Essentials comes in: It’s a common abstraction layer that mediates and manages access to those platform services without requiring custom code or platform-specific skills.

Getting started with Xamarin.Essentials

Xamarin.Essentials is a single cross-platform library that gives access to more than 25 platform APIs across iOS, Android, and Universal Windows Platform (UWP)—but not MacOS. You can use them in apps that target one or more of those platforms, letting code that’s been developed for, say, an Android audience be quickly ported to support iOS as well. Most current devices are supported, with the latest build handling iOS 10 and later, Android 4.4 KitKat and later, and UWP releases later than Build 1709. Like much of its .Net work, Microsoft is working on Xamarin.Essentials in the open, hosting it on GitHub. Although it’s not currently taking pull requests for features, Microsoft is looking for the community to help fix bugs and refactor the code.

The library is also designed to take advantage of features in the .Net linker and compiler, so code that’s not being used won’t be carried through a build. This approach reduces the size of your final native binaries, as well as keeps possible security risks to a minimum by giving your app as small an attack surface as possible.

Adding Xamarin.Essentials to your code is easy enough, because it’s available as a NuGet package and can be added through Visual Studio’s Solution Explorer. Make sure you’ve added it to all your projects, for all your target devices, and wait for it to install the package and any prerequisites. You also need to add a reference to it in all your C# classes that will use its APIs. Android apps do require additional configuration, so you need to add a little extra code to the default OnCreate method and to any Activity classes.

It’s important for a tool like this to have a simple setup. Using familiar platform tools ensures that developers can pick it up quickly. Similarly, with only a couple of lines of code needed to initialize Xamarin.Essentials, there’s very little learning required before you can start using its APIs in your apps. The initial set of APIs covers much of what you’ll need in a modern mobile app. They wrap most common device sensor platforms, as well as providing access to core OS functions and networking.

Calling Xamarin.Essentials APIs from your code

Once you’ve enabled Xamarin.Essentials support in an application, you can start to add APIs to your code. You will still need to set some device-specific properties, so keep separate projects for each target OS. Most of these will give your app specific permissions; for example, adding access to GPS sensors and location data if you’re using the geolocation APIs.

You’ll find most of the API calls logical, and their responses are what you’d expect. For example, make a location call to get an object that contains latitude, longitude, and altitude. You can take that data and pass it to a map control or save it in a database. Calls need to be asynchronous, because you need to wait for a response from the underlying OS APIs. There are also options to cancel actions if an app isn’t going to complete them, letting you handle shutdown and suspend operations gracefully.

APIs in Xamarin.Essentials aren’t complex, and some are very simple, such as the SMS messaging API. All you need to do is call its ComposeAsync method with a message and recipient, to automatically create a message in the default SMS client. You don’t need to know how the underlying OS does things; all you need to know are the abstracted Xamarin methods.

The result is an approach that should lead to quicker development and, more important, fewer bugs. The existing Xamarin plugin-based extension model isn’t going away, and it is the preferred way to support more complex API interactions. What you get with this release is a core set of APIs that should cover most basic device and OS interactions, helping you ship code faster.

With Xamarin.Essentials, Microsoft has covered many of the APIs modern mobile apps need. By abstracting away from the underlying OS it’s also made it easier to work with data, returning it in logical formats that simplify writing code that goes from API to UI and back again. The combination of Xamarin.Essentials and Xamarin Forms should finally give Microsoft the end-to-end cross platform development story it’s been seeking for PCs and mobile devices.

Copyright © 2018 IDG Communications, Inc.