/* */

First look at Xamarin.Essentials

Xamarin.Essentials was casually name-dropped by Miguel and James during their sessions at Build, without being officially announced. This might be because the bits are still in Preview and they’re still working on it, but it’s looking great already! Last week the first word was out, so let’s have a first look at Xamarin.Essentials.

Originally called Caboodle, Xamarin.Essentials is a single library to access native platform features for iOS, Android, and UWP that works in traditional Xamarin and Xamarin.Forms apps. Before Essentials, you had to grab one or more of the many plugins from James Montemagno. Now we can access them through one, single codebase: Xamarin.Essentials. There is already some extensive documentation available so it’s easy to play around with.

Getting started

Here, I’ll use Xamarin.Forms as starting point but native Xamarin.iOS or Xamarin.Android will work as well. Simply add the Xamarin.Essentials NuGet package to all Projects (iOS, Android and .NET Standard). Since it’s currently in Preview, make sure you mark the Show pre-release packages checkbox in the Add Packages window.

Once you got the package in place, there’s some additional stuff you have to do to get it working on Android. We’ll need to initialise Essentials in the MainActivity and also override the OnRequestPermissionsResult like so:


protected override void OnCreate(Bundle bundle)
{
    ...
    global::Xamarin.Forms.Forms.Init(this, bundle);
    Xamarin.Essentials.Platform.Init(this, bundle);
    ...
}

public override void OnRequestPermissionsResult(int requestCode, string[] permissions,
    [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
{
    Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
    base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}

That’s all there is to it, we now can start using Xamarin.Essentials!

Sample

I created a very simple sample app that allows you to send a SMS message using Xamarin.Forms and Xamarin.Essentials. I created a very basic UI with two Entry-fields and one Button to submit. You can view the complete source on Github to directly to dive into the code. But this is all it takes to send a SMS using Xamarin.Essentials:


private async void submitBtn_Clicked(object sender, EventArgs e)
{
    var message = messageEntry.Text;
    var recipient = recipientEntry.Text;

    var smsMessage = new SmsMessage(message, recipient);
    await Sms.ComposeAsync(smsMessage);
}

Simple and very straightforward! James wrote a sample Compass-app as well and demonstrated it at Build to get even more info.

Conclusion

Even though Essentials is still in Preview, this is exactly what I wanted to have as a mobile developer. One package with all the cross-platform plugins in one place. I can’t wait for the package to be Stable, but it’s looking great so far! Don’t forget to check out my sample on Github. Jim Bennett also wrote about Xamarin.Essentials. What do you think of this library? What’s missing or would you like to see different? I can’t wait to see where this library will head to!

Leave a reply:

Your email address will not be published.

Site Footer