We announced Xamarin.Essentials, a core set of cross-platform APIs to help developers build native apps, at Microsoft Build 2018. Xamarin.Essentials gives 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 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.
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(object sender, 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.
}
}
}
MainThread APIs
When developing applications that process information in the background, it’s important to update the user interface on the main thread. With the MainThread 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.
using Xamarin.Essentials;
public class MyViewModel
{
public bool UpdateUI()
{
MainThread.BeginInvokeOnMainThread(() =>
{
// Ensure invoked on MainThread. Xamarin.Essentials will optimize this and check if you are already on the main thread
});
}
public bool ProcessInformation()
{
if(MainThread.IsMainThread)
{
// Start new thread to process on background
}
else
{
// Already on non-main thread.
}
}
}
Simplified Android Dependencies
Based on your feedback, Xamarin.Essentials is 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.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.
Learn More
Read more about this release in our full release notes and be sure to browse through our full documentation, which has a full overview of how to get started and how to use every feature of Xamarin.Essentials.
0 comments