【发布时间】:2018-01-05 17:40:53
【问题描述】:
我有一个为 Windows 创建的应用:PC/Tablet 8.1、Mobile/Phone 8.1 和 UWP 10。
这是一个使用 C# 的 WinRT 应用程序。
要在应用中放置广告横幅,需要为每个操作系统制作单独的广告单元 ID。
有没有办法确定当前正在使用哪个操作系统?
可以通过代码查看正在使用的设备:
#if WINDOWS_PHONE_APP
isWindowsPhoneApp = true;
#else
isWindowsPhoneApp = false;
#endif
但是如何知道操作系统是Windows 8.1还是Windows 10呢?
更新:
我看到了一篇关于获取 C#/XAML 操作系统版本的有趣文章:
Windows Store Apps: Get OS Version, beginners tutorials (C#-XAML)
它使用 System.Type.GetType 来检查 Windows.System.Profile.AnalyticsVersionInfo 是否返回 null。
我修改并测试了代码,它似乎在 Visual Studio 模拟器和模拟器中工作。由于我使用的是 Windows 10 计算机,因此我无法测试 Windows 8.1 计算机,但对于 Windows Phone 8.1 和 Windows 10 Mobile,它是准确的。我还没有在实际的手机设备上测试过。
因此,检查仅在 Windows 10 中可用的 AnalyticsVersionInfo 类型似乎将根据操作系统返回 true 或 false。
那么建议在发布版本中使用以下代码吗?
var analyticsVersionInfoType = Type.GetType("Windows.System.Profile.AnalyticsVersionInfo, Windows, ContentType=WindowsRuntime");
var isWindows10 = analyticsVersionInfoType != null;
displayTextBlock.Text = "Is Windows 10: " + isWindows10;
更新:
单线:
var isWindows10 = Type.GetType("Windows.System.Profile.AnalyticsVersionInfo, Windows, ContentType=WindowsRuntime") != null;
【问题讨论】:
标签: c# windows-runtime operating-system