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

5

我需要在我的WP8.1应用程序代码中检查操作系统版本(WP 8.1或W10)。有什么更好的方法可以实现这一点吗?也许可以使用反射或一些特殊API来实现这个目的?


WP8.1可能无法安装W10应用程序。如果您为平台创建具有不同配置的应用程序,也许可以使用预处理器指令? - Romasz
@Romasz 我在 WM10 模拟器上测试了我的 WP8.1 应用程序,它可以正常工作。而且一些用户已经在他们的 W10 设备上安装了我的应用程序。我不想在未来创建特殊的 UWP 版本之前创建不同的构建配置。我想在代码中检查版本。 - Boris Salimov
好的,所以你只需要检查WP8.1是否在W10上运行即可。起初我考虑使用此OS版本,但经过测试发现它对于两个操作系统都返回“WindowsPhone”。 - Romasz
@Romasz,你说得对,这些方法对于两个操作系统都返回“WindowsPhone”。不幸的是,我还没有找到解决问题的方法。 - Boris Salimov
2个回答

4

我没有找到其他方法,所以这是我的做法。

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

 #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

它是如何工作的?

在Windows 8.1中,Package类具有DisplayName属性,而在Windows Phone 8.1中不存在该属性。在Windows Phone 8.1中,DisplayInformation类具有RawPixelsPerViewPixel属性,而在Windows 8.1中不存在该属性。Windows 10(包括移动版)都具有这两个属性。这就是我们可以检测应用程序运行的操作系统的方法。


-1

2
很遗憾,WP8.1(XAML)不支持在Environment类中使用属性OSVersion。 - Boris Salimov

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接