Xamarin.Essentials abstracts native mobile features into a set of cross-platform APIs with the aim to simplify their use. After a few months in preview, it has been now officially released with a number of enhancements contributed by the community.
Xamarin has long provided a way to use native features offered by the host platform. This comes in the form of bindings that expose the native API in C#. For example, iOS features such as ARKit, accelerometer, and even the Objective-C runtime are available through the Xamarin.iOS framework. This approach allows developers to use native features using the actual iOS, Android, or macOS APIs &emdash; only they can do it without leaving C#. Xamarin.Essentials pursues a different approach based on a single API that is the same across all supported platforms. This has the advantage that a larger part of your business logic can stay generic. According to Microsoft Xamarin engineer James Montemagno, Xamarin.Essentials is highly optimized and the Xamarin linker will only link the features you actually use and strip the rest.
Currently, Xamarin.Essentials includes over 30 native features, spanning from connectivity to geolocation, from device information to secure storage, and more.
Many of the APIs included in Xamarin.Essentials attempt to share a similar design. For example, the magnetometer, gyroscope, compass, and others all require you to use the
stop methods to start or stop to listen for changes on the corresponding sensor, with changes sent back through the
ReadingChangedevent. Other Essentials APIs follow a more imperative style. For example, to turn the flashlight on or off, securely store a sequence of bytes, or execute a piece of code on the main thread, you just call the corresponding method. The main differences across platforms lies with the different permission systems that each platform adopts and the varying requirements an app must fulfill. Those are thoroughly documented along with each API, though.
Xamarin.Essentials requires Android 4.4 or higher, iOS 10.0 or higher, or UWP 10.0.16299.0 or higher. You can add Xamarin.Essentials to your project using NuGet and reference it from your source files adding
using Xamarin.Essentials;. If you already have a Xamarin app and you would like to switch to Xamarin.Essentials, do not miss the transition guide written by Montemagno. Otherwise a great starting point is the official Xamarin GeoContacts app.