Top 5 APIs in Xamarin Essentials you didn’t know about

xamessentials
Share on facebook
Share on twitter
Share on linkedin

If you are familiar with Xamarin Essentials and want to get right into the totally not clickbait list you can click right here.

Xamarin Essentials provides developers with cross platform access to a plethora of commonly used features and native APIs. At the moment of writing there are 29 apis and growing. Some of these features used to be pretty annoying to setup but now you don’t have to do any of that and can just focus on the business logic of your application. The best part is that if you don’t use a feature it won’t be included after compiling so there is no fear of fat output files.

Xamarin.Essentials supports the following platforms and operating systems:

Platform Version
Android 4.4 (API 19) or higher
iOS 10.0 or higher
UWP 10.0.16299.0 or higher

The setup is pretty clearly explained here and since version 1.0 of Xamarin Essentials the package comes included in every new Xamarin Forms project. That being the case, not taking advantage of it may be a huge mistake and can lead to sadness induced by lack of sleep or frustration.

To avoid some people from suffering of sleep deprivation I have compiled this list of 5 obscure features you can get access to using Xamarin Essentials right out of the gate.

But first…

I will not cover these features in depth, however I will include a gist for each one and a github repository with an example running all together.

Most of these features are not available in all devices and Xamarin Essentials will raise a FeatureNotSupportedException when you try to access a feature that is not supported by your device. You can use services like GSMArena to check the range of devices that have certain feature available.

Android Emulatiors have a very good tool to play with sensor values, so if your physical device doesn’t include on of these features you can always use that tool.

The relative position sensors screenon Android emulator
Other sensor measurements on Android emulator

When working with sensors features on Xamarin Essentials you will probably notice the following:

  • All sensors can be accessed by name
  • Every sensor can be Started and Stopped
  • There is a property to check if we are currently measuring it called IsMonitoring, we have to check it before starting the sensor because if we don’t we will raise an exception.
  • The speed for monitoring changes
  • A ReadingChanged event

Let’s get started

1. The barometer

There is no shame to admit it, I just learned what a barometer was for writing this post. A barometer is an instrument used for measuring atmospheric pressure, used especially in forecasting the weather and determining altitude. Here is a list of devices with this hardware feature included.

2. The Gyroscope

The gyroscope sensor let’s you check the rotation of the device among the three axes X, Y and Z. You can subscribe to the reading change event and respond to the rotation change. The rotation value is calculated against a delta so if the device is still this value should remain 0 but, because the earth is always vibrating. This value is always changing ever so little which you need to take this into consideration when using it.

Have you seen an app that moves when you move? Like say Pokemon Go or other AR games. Those apps use the gyroscope sensor to calculate where you are in relation to other things.

3 . The Magnetometer

This one is more or less like the gyroscope but it calculates the device’s orientation relative to Earth’s magnetic field.

4. Geocoding

Not to be confused with Geolocation this one is pretty cool and quite useful. The Geocoding API let’s you convert a placemark (that is a place address in plain text) to a positional coordinate (latitudes and longitudes) and reverse geocode coordinates to a placemark.

5. Shake detection / Accelerometer

You probably already know that most devices have an Accelerometer built in and as expected Xamarin Essentials does include an Accelerometer class, what you probably didn’t know is that Xamarin Essentials implementation can also let you register an event handler for when the user shakes the device. Let’s take a look:

And that’s about it for now. I’ll leave with a reminder that Xamarin Essentials is an Open Source project and it is maintained by people like you and me. So I want to encourage you to go to the repository, check the issues, fork it and submit some code. Send a couple of PR’s and have fun with the features we have already.

Did you like the post and Wanna leave a reply? You can mention me on twitter @eatskolnikov so we can have a conversation about it

Share it with your friends

Share on facebook
Share on twitter
Share on linkedin

Want to get these post in your inbox?

Type your email down here and you will get a weekly digest of the post here

Other articles on the site

ios

Layout safe areas for iOS in Xamarin Forms

Since the launch of the iPhone X there is a notch at the top of the screen on iOS devices that tend to be annoying to work around. You would have to add some sort of margin to avoid your controls to be overlapped by it. The problem with this

Read More »
From https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/images?tabs=macos
caching

Image caching in Xamarin Forms

Someone asked me about an issue they were having when showing a ListView that had an image for each cell. When the user would scroll the images would flicker and the UI would turn a bit slow. The images were being loaded from the internet. The problem the person was

Read More »