【问题标题】:Could I know if my app is running on Windows RT with C#?我能否知道我的应用程序是否在使用 C# 的 Windows RT 上运行?
【发布时间】:2013-06-03 05:54:36
【问题描述】:

我知道无法在 Windows Store 应用中获取操作系统版本,请让我解释一下。

我的应用是使用 C# 编程的 Windows 应用商店应用。我的应用程序的某些功能依赖于另一个桌面应用程序(也许它不是一个好的设计)。据我所知,第三方桌面应用程序无法安装在 Windows RT 上,所以我只想知道我的应用程序是否在 Windows RT 上运行,并禁止我的应用程序在 Windows RT 上的某些功能。我不想使用 GetNativeSystemInfo(),因为它是一个 win32 API,如果我使用该 API,我的应用程序无法与任何 cpu 兼容。

【问题讨论】:

    标签: c# windows-8 windows-store-apps winrt-xaml


    【解决方案1】:

    我不知道这是否真的可能,但是您可以使用“条件编译符号”来实现结果。例如,您可以添加 ARM 仅用于 ARM 构建,并构建 3 个单独的包:ARM、x86 和 x64。 你可以找到很多关于如何使用它的文章。这是其中之一 Visual Studio:Use Conditional Compilation to Control Runtime Settings for Different Deployment Scenarios

    因此,您只需为您的项目设置 ARM 配置特殊符号 ARM(就像在此屏幕截图中看到的 LIVE)。

    在此之后,您可以使用 C# 指令:#if, #else, #endif 隐藏部分代码以用于 ARM 构建。

    【讨论】:

    • 您好,outcoldman,谢谢您的回复。这确实是一个解决方案,我想把它作为我最后的选择。但是我真的很想想办法只用一个包来解决这个问题。
    【解决方案2】:

    由于您的应用的某些功能依赖于桌面应用(可能已安装或未安装),因此即使在 x86 版本的应用中,您也需要处理“未安装”状态。

    我要做的是默认禁用所有这些功能,并在每次启动时检查桌面应用程序是否存在。只有当它存在时,我才会启用附加功能。

    【讨论】:

    • 不,W8 应用程序不是桌面或 Metro。他们只有一个,地铁。虽然我们不再称它为 Metro,但在这种情况下它是最清晰的方式。
    • 1. OP提到了“桌面应用程序”和“Windows Store应用程序”。 2.“Windows Store 应用”依赖于“桌面应用”。 3.“桌面应用程序”可能运行或不运行;可能存在或不存在。这与“Windows Store 应用”的处理器架构无关。
    【解决方案3】:

    是的。方法如下!

    Task<ProcessorArchitecture> WhatProcessor()
    {
        var t = new TaskCompletionSource<ProcessorArchitecture>();
        var w = new WebView();
        w.AllowedScriptNotifyUris = WebView.AnyScriptNotifyUri;
        w.NavigateToString("<html />");
        NotifyEventHandler h = null;
        h = (s, e) =>
        {
            // http://blogs.msdn.com/b/ie/archive/2012/07/12/ie10-user-agent-string-update.aspx
            // IE10 on Windows RT: Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; ARM; Trident/6.0;)
            // 32-bit IE10 on 64-bit Windows: Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)
            // 64-bit IE10 on 64-bit Windows: Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Win64; x64; Trident/6.0)
            // 32-bit IE10 on 32-bit Windows: Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0) 
            try
            {
                if (e.Value.Contains("ARM;"))
                    t.SetResult(Windows.System.ProcessorArchitecture.Arm);
                else if (e.Value.Contains("WOW64;") || e.Value.Contains("Win64;") || e.Value.Contains("x64;"))
                    t.SetResult(Windows.System.ProcessorArchitecture.X64);
                else
                    t.SetResult(Windows.System.ProcessorArchitecture.X86);
            }
            catch (Exception ex) { t.SetException(ex); }
            finally { /* release */ w.ScriptNotify -= h; }
        };
        w.ScriptNotify += h;
        w.InvokeScript("execScript", new[] { "window.external.notify(navigator.userAgent); " });
        return t.Task;
    }
    

    祝你好运!

    【讨论】:

    • 嗨,杰瑞,非常好的代码。我考虑过webview,但不如你的好。我会试试的,非常感谢。
    • 太好了,我在我的 Surface RT 上测试了它;有用。当然。
    • 对于 Windows 8.1(使用 IE11),您应该使用 eval 而不是 execScriptMSDN
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-21
    • 1970-01-01
    • 1970-01-01
    • 2011-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多