如何检测在Windows 10 Mobile上启动的WP8.1应用程序?

Bor*_*mov 5 windows-phone-8.1 windows-10-mobile uwp

我需要在我的WP8.1应用程序代码中检查操作系统版本(WP 8.1或W10).有什么更好的方法呢?为此目的可能是反思或一些特殊的API?

Art*_*ous 4

我没有找到任何其他方法来做到这一点,所以这是我的方法。

以下属性IsWindows10检测 Windows 8.1 或 Windows Phone 8.1 应用程序是否正在 Windows 10(包括 Windows 10 Mobile)设备上运行。

 #region IsWindows10

    static bool? _isWindows10;
    public static bool IsWindows10 => (_isWindows10 ?? (_isWindows10 = getIsWindows10Sync())).Value;

    static bool getIsWindows10Sync()
    {
        bool hasWindows81Property = typeof(Windows.ApplicationModel.Package).GetRuntimeProperty("DisplayName") != null;
        bool hasWindowsPhone81Property = typeof(Windows.Graphics.Display.DisplayInformation).GetRuntimeProperty("RawPixelsPerViewPixel") != null;

        bool isWindows10 = hasWindows81Property && hasWindowsPhone81Property;
        return isWindows10;
    }
 #endregion
Run Code Online (Sandbox Code Playgroud)

它是如何工作的?

在 Windows 8.1 中,该类Package具有DisplayNameWindows Phone 8.1 所没有的属性。在 Windows Phone 8.1 中,该类DisplayInformation具有RawPixelsPerViewPixelWindows 8.1 所没有的属性。Windows 10(包括移动版)具有这两个属性。这就是我们如何检测应用程序正在运行的操作系统。