(翻译)Xamarin.Essentials 最新预览版的更多跨平台 API

原文地址:https://blog.xamarin.com/cross-platform-apis-xamarin-essentials-latest-preview/

在 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,今天你就可以试用了。

定位传感器

Xamarin.Essentials 第一个预览版提供给开发者访问加速计、陀螺仪、磁力计和指南针。根据你们的反馈,我们增加了一个定位传感器 API。这个 API允许你订阅读读取到报告回来的四元数变化,它描述了地球坐标相对于设备坐标的旋转。这在创建需要访问 3D 空间的应用时非常有用。

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.
        }
    }
}


主线程 API

当开发在后台处理信息的应用时,在主线程上更新用户界面时很重要的。使用主线程 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 依赖

基于你们的反馈,Xamarin.Essentials 现在是针对 Android 8.1(API27)构建的,附带更新了2个 Android Support 库的依赖,CustomTabs 和 Core.Utils。这意味着你需要确认你的 Xamarin.Android 项目针对 Android 8.1(API27)编译,可以在项目的属性中设置。

请记住,在项目中拥有所有 Android Support 库的版本很重要,现在是更新你的项目中依赖的好时机。

Xamarin.Essentials 行动

准备好了解更多关于 Xamarin.Essentials 了嘛?没有比观看最新的 Xamarin Show 更进一步。Snack Pack 给出了 Xamarin.Essentials 的全面概述和如何开始集成到你的应用中。

视频地址(如果你可以看到): https://www.youtube.com/watch?v=uGby6JBG2pY

了解更多

在我们的完整发行说明可以阅读更多关于这个版本的信息并浏览我们完整的文档来确认,有一个全面的概述和如何使用 Xamarin.Essentials 的每一个特性。

原文地址:https://www.cnblogs.com/heyixiaoran/p/9367015.html