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

Windows phone 8.1 如何检测Windows 10 Mobile上启动的WP8.1应用程序?,windows-phone-8.1,uwp,windows-10-mobile,Windows Phone 8.1,Uwp,Windows 10 Mobile,我需要在我的WP8.1应用程序代码中检查操作系统版本(WP8.1或W10)。还有什么更好的方法?可能是反射或为此目的的某些特殊API 试试这个我没有找到其他方法,所以我的方法是这样的 以下属性检测Windows 10(包括Windows 10 Mobile)设备上是否正在运行Windows 8.1或Windows Phone 8.1应用程序 它是如何工作的? 在Windows 8.1中,包类有一个显示名属性,而Windows Phone 8.1没有该属性。 在Windows Phone 8.1中

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

试试这个

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

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

它是如何工作的?

在Windows 8.1中,
类有一个
显示名
属性,而Windows Phone 8.1没有该属性。 在Windows Phone 8.1中,
DisplayInformation
类有一个
RawPixelsPerViewPixel
属性,而Windows 8.1没有该属性。
Windows 10(包括Mobile)具有这两个属性。这就是我们可以检测应用程序运行在哪个操作系统上的方法。

WP8.1可能无法安装W10应用程序。如果您为平台创建具有不同配置的应用程序,也许您可以使用预处理器指令?@Romasz我在WM10 Emulator上测试了我的WP8.1应用程序,它可以工作。一些用户已经在他们的W10设备上安装了我的应用程序。我不想在将来创建特殊的UWP版本之前创建不同的构建配置。我想检查代码中的版本。好的,所以您只需要检查WP8.1是否在W10上运行。首先我想到了,但在测试中,它对两个操作系统都返回“WindowsPhone”。@Romasz您是对的,方法对两个操作系统都返回“WindowsPhone”。不幸的是,我还没有找到问题的解决方案。不幸的是,WP8.1(XAML)在类环境中不支持属性OSVersion。
 #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