【问题标题】:How to detect Windows Phone 8.1 OS version programmatically?如何以编程方式检测 Windows Phone 8.1 操作系统版本?
【发布时间】:2014-07-24 06:45:57
【问题描述】:

标题中的问题不是真正的问题。我浏览了许多网站和博客,了解到 Environment.OSVersion 使用我们的应用程序为您提供了手机的当前操作系统版本。但问题是,Environment 类下没有 OSVersion。请参阅屏幕截图以更好地理解。

我的问题是为什么我看不到 Environment 类下的 OSVersion 属性?我错过了什么吗?

【问题讨论】:

标签: c# .net-4.5 windows-phone-8.1 win-universal-app


【解决方案1】:

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

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

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

【讨论】:

【解决方案2】:

复制自受骗问题:

Windows Phone 8.1 Silverlight 应用可以使用 .NET 版本的 API。通用 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);

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

【讨论】:

  • 非常感谢!对于我的双平台 8.1+10 代码,这个方法可以正常工作——如果没有那个类型,我只返回 8.1。
  • 注意:如果GetRuntimeProperty 丢失,请将using System.Reflection; 添加到您的使用中。
【解决方案3】:

您无法在 Windows 8.1 中获取操作系统版本。请查看以下链接以获取相同信息 - https://social.msdn.microsoft.com/Forums/windowsapps/en-US/2b455331-3bad-4d26-b615-a59d0e05d0dd/how-to-get-os-version-on-window-phone?forum=wpdevelop

【讨论】:

    【解决方案4】:

    我发现了一种检测设备运行的是 Windows Phone 8.1 还是 Windows Phone 10 的棘手方法。我比较了 3 种不同的设备,诺基亚 Lumia 925 (wp 8.1)、诺基亚 Lumia 735 (wp 10) 和诺基亚 Lumia 930(第 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;
            }
    

    我还没有机会在其他设备上对此进行测试,但到目前为止似乎可行。我用它来区分 Windows Phone 8.1 和 Windows Phone 10 之间的应用评分功能的代码,在我的具体情况下不是 UWP

    希望对你有帮助

    【讨论】:

      【解决方案5】:

      如果您的应用基于 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;
              }
          }
      

      【讨论】:

        【解决方案6】:

        只需使用这一行来获取应用程序名称和 ID、发布者名称等...

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2016-10-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-02-05
          相关资源
          最近更新 更多