原文地址:https://blog.xamarin.com/cross-platform-apis-xamarin-essentials-latest-preview/api
在 Microsoft Build 2018 咱们宣布了 Xamarin.Essentials,一个帮助开发者构建原生应用的跨平台 API 核心套件。Xamarin.Essentials 提供给开发者经过共享代码访问超过30个特定平台的 API,包括地理位置、安全存储、传感器、设备信息和更多其余的 API。最重要的是,Xamarin.Essentials 能够用在任意的 iOS、Android、UWP 或者 Xamarin.Forms 应用,无论你如何建立用户界面。来自开发者的第一个预览版的反馈很是棒,访问这些原生特性的直截了当的方式受到了一致的赞赏。安全
今天,咱们很高兴发布 Xamarin.Essentials(0.7.0-preview)的第二个预览版,今天在 NuGet 上就能够使用了。这个版本结合了开发者的反馈、bug 修复和几个新 API,今天你就能够试用了。优化
定位传感器ui
Xamarin.Essentials 第一个预览版提供给开发者访问加速计、陀螺仪、磁力计和指南针。根据大家的反馈,咱们增长了一个定位传感器 API。这个 API容许你订阅读读取到报告回来的四元数变化,它描述了地球坐标相对于设备坐标的旋转。这在建立须要访问 3D 空间的应用时很是有用。this
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. } } }
主线程 APIspa
当开发在后台处理信息的应用时,在主线程上更新用户界面时很重要的。使用主线程 API,如今能够访问检测当前线程是否在主线程上,并开始在主线程上进行更新。咱们在内部使用这个 API 来优化 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. } } }
简化的 Android 依赖code
基于大家的反馈,Xamarin.Essentials 如今是针对 Android 8.1(API27)构建的,附带更新了2个 Android Support 库的依赖,CustomTabs 和 Core.Utils。这意味着你须要确认你的 Xamarin.Android 项目针对 Android 8.1(API27)编译,能够在项目的属性中设置。orm
请记住,在项目中拥有全部 Android Support 库的版本很重要,如今是更新你的项目中依赖的好时机。视频
Xamarin.Essentials 行动
准备好了解更多关于 Xamarin.Essentials 了嘛?没有比观看最新的 Xamarin Show 更进一步。Snack Pack 给出了 Xamarin.Essentials 的全面概述和如何开始集成到你的应用中。
视频地址(若是你能够看到): https://www.youtube.com/watch?v=uGby6JBG2pY
了解更多
在咱们的完整发行说明能够阅读更多关于这个版本的信息并浏览咱们完整的文档来确认,有一个全面的概述和如何使用 Xamarin.Essentials 的每个特性。