Xamarin.Essentials Adds Detect Shake, Browser Customization, Color Helpers, and More!

James Montemagno

Xamarin.Essentials is your ultimate open source library to access native features from a single cross-platform API. Since the first release, we have received awesome feedback on GitHub from developers that integrated it into their apps. We have also received some great pull requests to add some new features and bug fixes that have gone into the newest release 1.1.0 that are available today. This includes the ability to detect shake movement of the device, customization when opening the browser, ability to check for mock location, and several platform helpers.

Detect Shake

Sometimes you need to detect shake movement of a device that your app is running on to open a feedback form or put the app into edit mode. Xamarin.Essentials’ built-in Accelerometer API gives you everything you need to calculate this cross-platform. Having to figure out all of the math and queue systems could be a bit much. So we figured why not just create a unique ShakeDetected event that you can register for. You will still have to start the Accelerometer, but now you have the option to get all changes or just when a shake is detected:

public override void OnAppearing()
    Accelerometer.ShakeDetected += Accelerometer_OnShaked;

void Accelerometer_OnShaked(object sender, EventArgs e)
    ShakeTime = $"Shake detected: {DateTime.Now.ToLongTimeString()}";

public override void OnDisappearing()
    Accelerometer.ShakeDetected -= Accelerometer_OnShaked;

Browser Customization

When you need to launch a website the best approach is to use the built-in Open Browser from Xamarin.Essentials. It will automatically handle opening the system optimized browser with SFSafariViewController and Chrome Custom Tabs. In the new release of Xamarin.Essentials you now have more control over the look. You can also get the feel of the browser including the title visibility and the colors of the toolbar and controls.

await Browser.OpenAsync(uri, new BrowserLaunchOptions
LaunchMode = BrowserLaunchMode.SystemPreferred,
TitleMode = BrowserTitleMode.Show,
PreferredToolbarColor = Color.AliceBlue,
PreferredControlColor = Color.Violet

Platform Helpers

The core goal of Xamarin.Essentials is to simplify cross-platform app development. Most of the APIs that are part of the library up until now have abstracted common platform features into a single API. Xamarin.Essentials 1.0 launched with a plethora of built-in unit converters such as converting Miles to kilometers or Celsius to Fahrenheit. With this release, we are pleased to introduce several key helper utilities for converting between .NET and platform structures such as Color, Rect, Size, and Point.

Color Converters

For System.Drawing.Color we want to bring all of the goodies that Xamarin.Forms developers are able to take advantage of such as creating color from Hsl, Hex, or UInt.

var blueHex = ColorConverters.FromHex("#3498db");
var blueHsl = ColorConverters.FromHsl(204, 70, 53);
var blueUInt = ColorConverers.FromUInt(3447003);

Additionally, you can easily convert System.Drawing.Color to the platform specific color structure:

var system = System.Drawing.Color.FromArgb(255, 52, 152, 219);

// Extension to convert to Android.Graphics.Color, UIKit.UIColor, or Windows.UI.Color
var platform = system.ToPlatformColor();
var platform = new Android.Graphics.Color(52, 152, 219, 255);

// Back to System.Drawing.Color
var system = platform.ToSystemColor();

Rect, Size, and Point Converters

Just like the Color extension methods to convert to and from the platform specific classes can be done with Size, Rect, and Point:

var system = new System.Drawing.Size(width, height);

// Convert to CoreGraphics.CGSize, Android.Util.Size, and Windows.Foundation.Size
var platform = system.ToPlatformSize();

// Back to System.Drawing.Size
var system2 = platform.ToSystemSize();

The same can be done for Rect with ToPlatformRectangle and ToSystemRectangle and for Point with ToPlatformPoint and ToSystemPoint.

Community Contributions

We want to call out the amazing community contributions that helped make this release possible:

Contributor Description

Chris Dinon

Use Apply on Android Preferences (#636)

Niklas Schilli

Browser Customization APIs (#196) Fix Launcher Sample (#629)

Pedro Jesus

Add IsFromMockProvider for Geolocation (#676) Detect Shake API (#126)

We have also received several great bug reports and feature requests from the community, which has been great to see! If you are looking to help out simply head over to the Xamarin.Essentials GitHub page to open an issue or send a pull request.

Learn More

You can learn more by browsing the full Xamarin.Essentials documentation including new documentation for Detect Shake, Color Converters, Platform Extensions, Browser options, and more. Additionally, each week you can find a brand new Xamarin.Essentials API of the Week videos covering a single feature in just a few minutes.


Discussion is closed.

Feedback usabilla icon