Leveraging Xamarin.Essentials: Streamlining App Development with Powerful, Pre-built Components (PART 1)

Introduction

As a mobile app developer, you’re constantly looking for ways to simplify your development process while still delivering feature-rich, high-quality applications. Xamarin is an excellent choice for building cross-platform mobile apps, and Xamarin.Essentials is a powerful companion library that can save you time and effort by providing access to native APIs through a single, unified interface. In this blog post, we’ll explore the Xamarin.Essentials library, its most useful features, and how developers can easily integrate these components into their Xamarin projects.

What is Xamarin.Essentials?

Xamarin.Essentials is a library that provides developers with a wide range of cross-platform APIs to access native device features using a shared codebase. It eliminates the need to write platform-specific code for common app functionalities like accessing the device’s file system, geolocation, or connectivity information. Xamarin.Essentials is compatible with Xamarin.iOS, Xamarin.Android, and Xamarin.Forms projects, allowing you to streamline your app development process.

Getting Started with Xamarin.Essentials

To start using Xamarin.Essentials in your project, follow these simple steps:

Install the Xamarin.Essentials NuGet package: In Visual Studio, navigate to the NuGet Package Manager and search for ‘Xamarin.Essentials’. Install the package to your shared code project and your platform specific projects (iOS, Android, and UWP, if applicable).

Add the required initialization code:

For Android, open the MainActivity.cs file and add Xamarin.Essentials.Platform.Init in the OnCreate method:

protected override void OnCreate(Bundle savedInstanceState)
{

Xamarin.Essentials.Platform.Init(this, savedInstanceState);

}

For iOS, open the AppDelegate.cs file and add Xamarin.Essentials.Platform.Init in the FinishedLaunching method:

public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{

Xamarin.Essentials.Platform.Init();

}

For UWP, open the MainPage.xaml.cs file and add Xamarin.Essentials.Platform.Init in the MainPage constructor:

public MainPage()
{

Xamarin.Essentials.Platform.Init();

}

Update your app permissions: Based on the APIs you use, you may need to add specific permissions to your platform-specific projects. Refer to the Xamarin.Essentials documentation for the required permissions for each API.

Popular Xamarin.Essentials Features

Now that you’ve set up Xamarin.Essentials let’s explore some of its popular features and how to implement them in your Xamarin projects:

Geolocation

Obtain the user’s current location, calculate distances, and track location changes. To get the user’s location, use the Geolocation.GetLocationAsync method:

using Xamarin.Essentials;

public async Task GetCurrentLocationAsync()
{
try
{
var location = await Geolocation.GetLocationAsync();
return location;
}
catch (Exception ex)
{
// Handle exceptions
}
return null;

}

Connectivity

Check the user’s internet connection status and monitor for changes.

To check if the user has an active internet connection, use the Connectivity.NetworkAccess property:

using Xamarin.Essentials;


public bool IsConnectedToInternet()
{
var networkAccess = Connectivity.NetworkAccess;
return networkAccess == NetworkAccess.Internet;
}

To monitor connectivity changes, subscribe to the Connectivity.ConnectivityChanged event:

using Xamarin.Essentials;

public void SubscribeToConnectivityChanges()
{
Connectivity.ConnectivityChanged += OnConnectivityChanged;
}

private void OnConnectivityChanged(object sender, ConnectivityChangedEventArgs e)
{
var networkAccess = e.NetworkAccess;
// Handle connectivity changes
}

Device Information

Get information about the user’s device, such as the model, OS version, and screen metrics.

To obtain device information, use the properties provided by the DeviceInfo class:

using Xamarin.Essentials;

public string GetDeviceInformation()
{
string model = DeviceInfo.Model;
string manufacturer = DeviceInfo.Manufacturer;
string osVersion = DeviceInfo.VersionString;
DevicePlatform platform = DeviceInfo.Platform;

return $"Model: {model}\nManufacturer: {manufacturer}\nOS Version: {osVersion}\nPlatform: {platform}";

}

Conclusion

Xamarin.Essentials is a valuable tool that can help you create feature-rich, cross-platform mobile apps with a unified, shared codebase. By leveraging the powerful, pre-built components it offers, you can save time and effort, streamline your app development process, and focus on delivering a fantastic user experience. To explore more Xamarin.Essentials features, visit the official documentation. Happy coding!