Cross Platform Apis Xamarin Essentials Latest Preview

Cross Platform Apis Xamarin Essentials Latest Preview


We announced Xamarin. Essentials, a core set of cross-platform APIs to help developers build native apps, at Microsoft Build 2018. Xamarin.Essentials give developers access to over thirty platform-specific APIs that can be accessed from their shared code, including geolocation, secure storage, sensors, device information, and many more. Best of all, Xamarin.Essentials can be used in any iOS, Android, UWP, or Xamarin. Forms app, regardless of how you create the user interface. Feedback on the first preview from developers has been fantastic, with praise of a simple and straightforward way to access these native features.



Today, we are pleased to release our second preview of Xamarin. Essentials (0.7.0-preview), which is available today on NuGet. This release combines feedback from developers, bug fixes, and several new APIs that you can take advantage of today


Orientation Sensor

Xamarin.Essentials first release gave developers access to the accelerometer, gyroscope, magnetometer, and compass. Based on your feedback, we’ve added an API for the orientation sensor. This API allows you to subscribe to reading changes that are reported back as a Quaternion, which describes the rotation of the Earth’s coordinate system relative to the device’s coordinate system. This is extremely useful when creating applications that need access to 3D space.

// Code


public class OrientationSensorTest
{
// Set speed delay for monitoring changes.
SensorSpeed speed = SensorSpeed.Ui;
public OrientationSensorTest()
{
// Register for reading changes, be sure to unsubscribe when finished
OrientationSensor.ReadingChanged += OrientationSensor_ReadingChanged;
}
void OrientationSensor_ReadingChanged( AccelerometerChangedEventArgs e)
{
var data = e.Reading;
Console.WriteLine($”Reading: X: {data.Orientation.X}, Y: {data.Orientation.Y}, Z: {data.Orientation.Z}, W: {data.Orientation.W}”);
// Process Orientation quaternion (X, Y, Z, and W)
}
public void ToggleOrientationSensor()
{
try
{
if (OrientationSensor.IsMonitoring)
OrientationSensor.Stop();
else
OrientationSensor.Start(speed);
}
catch (FeatureNotSupportedException fnsEx)
{
// Feature not supported on device
}
catch (Exception ex)
{
// Other error has occurred.
}
}
}


Platform APIs

When developing applications that process information in the background, it’s important to update the user interface on the main thread. With the Platform API, you now have access to detect if you’re currently on the main thread and also begin an update on the main thread. We use this API internally to optimize our own code in Xamarin.Essentials.

# Code....



using Xamarin.Essentials;
public class MyViewModel
{
public bool UpdateUI()
{
Platform.BeginInvokeOnMainThread(() =>
{
// Ensure invoked on MainThread. Xamarin.Essentials will optimize this and check if you are already on the main thread
});
}
public bool ProcessInformation()
{
if(Platform.IsMainThread)
{
// Start new thread to process on background
}
else
{
// Already on non-main thread.
}
}
}



Simplified Android Dependencies

Based on your feedback, Xamarin.Essentials are now built against Android 8.1 (API 27), with the updated dependencies of two Android Support libraries, CustomTabs and Core. Utils. This means you will need to ensure you have your Xamarin. An Android project set to compile against Android 8.1 (API 27), which you can set in the properties of your project.

Remember, it’s important to have the saved version of all Android Support libraries in your project; now is a good time to update all of your dependencies in your project.

Xamarin.Essentials In Action

Ready to learn more about Xamarin.Essentials? Look no further than the latest Xamarin Show: Snack Pack, which gives a full overview of Xamarin. Essentials and how to start integrating it into your app.

Drop us a Query

About the author

Bhaskar

Bhaskar

The author has a keen interest in exploring the latest technologies such as AI, ML, Data Science, Cyber Security, Guidewire, Anaplan, Java, Python, Web Designing tools, Web Development Technologies, Mobile Apps, and whatnot. He bags over a decade of experience in writing technical content.

Corporate
whatsapp arrow
Loading...
// load third party scripts onload