c# - 如何在 UWP 应用程序中检查 Windows 10 操作系统版本以消除 WACK 测试失败?

标签 c# uwp windows-10 wack

我正在开发一个 UWP 应用程序,它使用了旧版 Windows 10 中不可用的一些功能。因此,我需要检查是否安装了创意者更新。

应用程序中有使用 AnalyticsInfo.VersionInfo 的版本检查代码。然而,最新一轮的 WACK 测试给出了以下失败:

FAILED Platform version launch • Error Found: The high OS version validation detected the following errors:

o Failed to stop the app AppName.group.mbb.

o The app Company.AppName_2.3.56045.0_x64__cx08jceyq9bcp failed platform version launch test.

• Impact if not fixed: The app should not use version information to provide functionality that is specific to the OS.

• How to fix: Please use recommended methods to check for available functionality in the OS. See the link below for more information. Operating System Version

我知道 this question , 但如果可能的话宁愿修复失败。 我在 MSDN 上找到了有关如何使 UWP 应用“版本自适应”的建议 here.

我现在有这段代码:

using Windows.Foundation.Metadata;

if (ApiInformation.IsMethodPresent("Windows.Networking.Connectivity.ConnectionProfile", "GetNetworkUsageAsync"))
    {
        //do stuff
    }

我的Windows版本是1703 15063.483,代码其他地方使用GetNetworkUsageAsync成功。但是对 IsMethodPresent 的调用总是返回 false。

我的代码有什么问题?
是否有更好的功能来检查是否安装了 Creators Update?

更新: 对于上述失败,我遵循了 Microsoft 指南,并将版本检查从 AnalyticsInfo.VersionInfo 更改为 Windows.Foundation.Metadata.ApiInformation。该应用程序仍未通过 WACK 测试并出现相同的错误。

第二次更新:
将 Windows10 更新到 Creators Update,Build 16251.0 后,此错误在我的计算机上消失了。

最佳答案

也许像这样的辅助类?请注意,调用此方法的成本可能很高,因此建议执行一次并缓存数据。

(更新:如果您使用 Windows Composition API,您会发现 AreEffectsFast and AreEffectsSupported 很有帮助,因为您可以使用它们根据用户的设备打开和关闭效果,操作系统条件。我扩展了下面的类以将它们公开为两个新属性。)

public class BuildInfo
{
    private static BuildInfo _buildInfo;

    private BuildInfo()
    {
        if (ApiInformation.IsApiContractPresent("Windows.Foundation.UniversalApiContract", 5))
        {
            Build = Build.FallCreators;
        }
        else if (ApiInformation.IsApiContractPresent("Windows.Foundation.UniversalApiContract", 4))
        {
            Build = Build.Creators;
        }
        else if (ApiInformation.IsApiContractPresent("Windows.Foundation.UniversalApiContract", 3))
        {
            Build = Build.Anniversary;
        }
        else if (ApiInformation.IsApiContractPresent("Windows.Foundation.UniversalApiContract", 2))
        {
            Build = Build.Threshold2;
        }
        else if (ApiInformation.IsApiContractPresent("Windows.Foundation.UniversalApiContract", 1))
        {
            Build = Build.Threshold1;
        }
        else
        {
            Build = Build.Unknown;
        }

        if (!BeforeCreatorsUpdate)
        {
            var capabilities = CompositionCapabilities.GetForCurrentView();
            capabilities.Changed += (s, e) => UpdateCapabilities(capabilities);
            UpdateCapabilities(capabilities);
        }

        void UpdateCapabilities(CompositionCapabilities capabilities)
        {
            AreEffectsSupported = capabilities.AreEffectsSupported();
            AreEffectsFast = capabilities.AreEffectsFast();
        }
    }

    public static Build Build { get; private set; }
    public static bool AreEffectsFast { get; private set; }
    public static bool AreEffectsSupported { get; private set; }
    public static bool BeforeCreatorsUpdate => Build < Build.Creators;

    public static BuildInfo RetrieveApiInfo() => _buildInfo ?? (_buildInfo = new BuildInfo());
}

public enum Build
{
    Unknown = 0,
    Threshold1 = 1507,   // 10240
    Threshold2 = 1511,   // 10586
    Anniversary = 1607,  // 14393 Redstone 1
    Creators = 1703,     // 15063 Redstone 2
    FallCreators = 1709  // 16299 Redstone 3
}

要初始化该类,请在 App.xaml.cs 中的 OnWindowCreated 之后立即调用它。

protected override void OnWindowCreated(WindowCreatedEventArgs args)
{
    BuildInfo.RetrieveBuildInfo();

要使用它,只需调用

if (BuildInfo.Build == Build.Anniversary) { ... }

if (BuildInfo.BeforeCreatorsUpdate) { ... }

if (BuildInfo.AreEffectsFast) { ... }

关于c# - 如何在 UWP 应用程序中检查 Windows 10 操作系统版本以消除 WACK 测试失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45480191/

相关文章:

c# - 序列化时如何按声明顺序获取类属性

c# - 防止在单元测试中写入数据库 - .NET MVC/Entity Framework

c# - 任何时候只能打开一个 ContentDialog

xaml - UWP/XAML : What is the difference between theming in a page's Resources, 在单独的 ResourceDictionary 文件中,还是在 App.xaml 中?

c# - BackgroundTask UWP Windows 10 TimeTriggeredTask 示例已注册但从未启动

winapi - 支持 ARM 上的 Windows 10 桌面应用程序 - MFC 和 COM 以及 OPOS 工作吗?

powershell - 为 Raspberry PI 2 构建 DefaultApp - Windows 10 IoT

c# - 查找所有祖先直到节点 id

c# - .NET:CLR 是否自动为堆分配对象引入基本线程安全(锁)?

xaml - UWP:如何从 View 模型中的另一个 ListView 捕获 ListView 部分的点击而不是代码隐藏?