如何以编程方式检测Windows Phone 8.1操作系统版本?

10
标题中的问题并不是真正的问题。我查看了许多网站和博客,并得知Environment.OSVersion可以给出使用我们的应用的手机的当前操作系统版本。但问题在于,在Environment类下没有OSVersion属性。请参考截图以更好地理解。
我的问题是:为什么我无法在Environment类下看到OSVersion属性?我有什么遗漏吗?

这应该会对你有所帮助:https://dev59.com/O2Af5IYBdhLWcg3w7mf3 - Sajeetharan
1
可能是重复问题 https://dev59.com/6mQm5IYBdhLWcg3wtAs_ - Jitendra
6个回答

13

通用/WinRT应用程序只能在wp 8.1中运行,因此操作系统版本只能是8.1。当他们制作wp8.2或wp9时,他们可能会添加一种检查已安装的操作系统版本的方法...

如果您正在寻找固件版本,则可以使用以下方式获取:

    Windows.Security.ExchangeActiveSyncProvisioning.EasClientDeviceInformation deviceInfo = new Windows.Security.ExchangeActiveSyncProvisioning.EasClientDeviceInformation();
    var firmwareVersion = deviceInfo.SystemFirmwareVersion;

问题是WP8.1有很多版本:http://en.wikipedia.org/wiki/Windows_Phone_version_history 有时候知道确切的版本非常有帮助,因为某些版本存在限制或者错误。 - Hong
1
然而,这并不适用于Windows 10 Mobile。 - Soheil Ghahremani

12

Windows Phone 8.1 Silverlight应用程序可以使用.NET版本API。在Universal 8.1应用程序中,没有受支持的机制来获取版本号,但是您可以尝试使用反射来获取Windows 10的AnalyticsInfo类,这将至少告诉您正在运行的版本号,如果您正在运行Windows 10。

注意:几乎总是检查操作系统版本是错误的,除非你只是向用户显示它(例如,在“关于”框中)或将其发送到后端分析服务器进行数字处理。它不应该用于任何运行时决策,因为通常它是一个较差的代理,用于实际要做的事情。

以下是一个示例:

var analyticsInfoType = Type.GetType(
  "Windows.System.Profile.AnalyticsInfo, Windows, ContentType=WindowsRuntime");
var versionInfoType = Type.GetType(
  "Windows.System.Profile.AnalyticsVersionInfo, Windows, ContentType=WindowsRuntime");
if (analyticsInfoType == null || versionInfoType == null)
{
  Debug.WriteLine("Apparently you are not on Windows 10");
  return;
}

var versionInfoProperty = analyticsInfoType.GetRuntimeProperty("VersionInfo");
object versionInfo = versionInfoProperty.GetValue(null);
var versionProperty = versionInfoType.GetRuntimeProperty("DeviceFamilyVersion");
object familyVersion = versionProperty.GetValue(versionInfo);

long versionBytes;
if (!long.TryParse(familyVersion.ToString(), out versionBytes))
{
  Debug.WriteLine("Can't parse version number");
  return;
}

Version uapVersion = new Version((ushort)(versionBytes >> 48),
  (ushort)(versionBytes >> 32),
  (ushort)(versionBytes >> 16),
  (ushort)(versionBytes));

Debug.WriteLine("UAP Version is " + uapVersion);

显然,您可以将其更新为返回版本等内容,而不是将其打印到调试控制台中。

1
非常感谢!对于我双平台的8.1+10代码,这种方法可以正常工作——如果没有那种类型,我就返回8.1。 - Soonts
1
注意:如果缺少GetRuntimeProperty,请在您的usings中添加using System.Reflection; - Heinrich Ulbricht

1

0

我找到了一种巧妙的方法来检测设备是否运行Windows Phone 8.1或Windows Phone 10。我比较了三个不同的设备,Nokia Lumia 925(wp 8.1),Nokia Lumia 735(wp 10)和Nokia Lumia 930(wp 10)。我注意到在wp8.1上没有设备信息ID(它会导致未实现的异常),但在测试的两个Windows Phone 10设备上存在。此外,系统固件版本格式在wp 8.1和wp 10之间似乎不同(第一个是xxxx.xxxxx.xxxx.xxxx,而第二个是xxxxx.xxxxx.xxxxx.xxxxx)。以下是我的函数:

        /// <summary>
        /// Indicates if this device is running a version of Windows Phone 8.1. It use a dirty trick for detecting the OS major version
        /// based on the system firmware version format (8.1 is xxxx.xxxxx.xxxx.xxxx while 10 is xxxxx.xxxxx.xxxxx.xxxxx )
        /// moreover, the "deviceInfo.id" is not implemented on Windows Phone 8.1, but it is on Windows Phone 10
        /// </summary>
        /// <returns></returns>
        public static bool liIsWindowsPhone81(bool basedOnDeviceInfoId)
        {
            EasClientDeviceInformation deviceInfo = new Windows.Security.ExchangeActiveSyncProvisioning.EasClientDeviceInformation();
            bool isWin81 = false;
            if (basedOnDeviceInfoId)
            {
                try
                {
                    var deviceInfoId = deviceInfo.Id;
                }
                catch
                {
                    isWin81 = true;
                }
            }
            else
            {
                string firmwareVersion = deviceInfo.SystemFirmwareVersion.Trim();
                string[] parts = firmwareVersion.Split('.');
                if (parts[0].Length == 4 && parts[1].Length == 5 && parts[2].Length == 4 && parts[3].Length == 4)
                {
                    isWin81 = true;
                }
            }

            return isWin81;
        }

enter image description here

我还没有机会在其他设备上测试这个,但目前看起来是有效的。我使用它来区分应用程序评级功能的代码在Windows Phone 8.1和Windows Phone 10之间,在我的特定情况下不是UWP。

希望这有所帮助。


0

如果您的应用程序是基于Silverlight的,您可以在Windows Phone 8.0、8.1以及Windows Mobile 10上使用System.Environment.OSVersion.Version。

下面是我们在确定是否显示自己的地理跟踪选择对话框还是让Windows Mobile 10呈现其自己的选择对话框时使用的方法示例。

    public static bool IsWindowsPhone8x()
    {
        try
        {
            Version version = System.Environment.OSVersion.Version;
            return version.Major > 8 ? false : true;
        }
        catch (Exception)
        {
            return false;
        }
    }

-2

只需使用此行代码即可获取应用程序名称、ID、发布者名称等信息...

string name = Windows.ApplicationModel.Package.Current.DisplayName;

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