使用 Uno Platform 在 class 库中获取设备信息

Getting device information in class library with Uno Platform

我正在尝试开发一个 class 库供 Uno Platform 应用程序使用,可以作为 NuGet 包分发。在代码中,我需要能够提取当前 运行 代码中有关设备的各种信息。我已经想出如何获取屏幕尺寸,但现在我被困在获取当前操作系统和版本上。我见过这样的代码:

#if __ANDROID__
    os = "android";
#elif __IOS__
    os = "ios";
#endif

但据我了解,class 库不支持此功能。我还尝试了 Xamarin.Essentials 的 Uno 端口来使用 DeviceInfo class。不过,我无法让它工作。

如有任何帮助,我们将不胜感激。

我以不同的方式获取 OS 信息。我使用以下代码获取 OS 信息:

public static string getOSVersion()
    {
        string osInfo = Windows.System.Profile.AnalyticsInfo.VersionInfo.DeviceFamily.ToString();
        int dotPosition = osInfo.IndexOf('.');
        string shortOsInfo = osInfo.Substring(0, dotPosition);
        return  shortOsInfo + " " + Windows.System.Profile.AnalyticsInfo.VersionInfo.DeviceFamilyVersion.ToString();
    }

如果你在 Android- 它将 return Android 11,在 IOS 它将 return iOS 14.4(保持请记住,版本号可能会有所不同 - 仅用于示例目的)。我在 WASM、UWP、Linux 或 MacOS 上没有 运行 这个,但它应该可以工作。希望对您有所帮助!