我需要在我的WP8.1应用程序代码中检查操作系统版本(WP 8.1或W10)。有什么更好的方法可以实现这一点吗?也许可以使用反射或一些特殊API来实现这个目的?
我需要在我的WP8.1应用程序代码中检查操作系统版本(WP 8.1或W10)。有什么更好的方法可以实现这一点吗?也许可以使用反射或一些特殊API来实现这个目的?
我没有找到其他方法,所以这是我的做法。
以下属性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(包括移动版)都具有这两个属性。这就是我们可以检测应用程序运行的操作系统的方法。