【问题标题】:Close an UWP app with URL scheme使用 URL 方案关闭 UWP 应用
【发布时间】:2018-06-20 11:42:30
【问题描述】:

我创建了一个适用于 Windows (UWP) 的示例应用程序并注册了一个 URL 方案

我的计划

它有一个 WebView,其源代码是我的自定义 URL,例如

http//:192.168.1.25:8080/dummywebsite.html

要打开应用程序,我使用的是 win+r 并输入 myscheme://hello 应用程序就会打开

我正在使用此 URL 处理关闭应用程序 myscheme://close

问题是当我打电话给window.location = "myscheme://close"; 它询问您要切换应用程序,然后当我单击“是”时它正在关闭应用程序。为什么我已经在该应用程序中时要求切换应用程序

如何直接从外部 javascript 关闭应用程序或删除该确认以切换应用程序

MainPage.xaml

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" >
        <WebView x:Name="WebView1" Source="http//:192.168.1.25:8080/dummywebsite.html" ScriptNotify="WebView1_ScriptNotify" />
    </Grid>

App.xaml.cs(关闭逻辑)

protected override void OnActivated(IActivatedEventArgs args)
    {
        // calls when qlicket url is fired 
        if (args.Kind == ActivationKind.Protocol)
        {
            ProtocolActivatedEventArgs eventArgs = args as ProtocolActivatedEventArgs;
            System.Diagnostics.Debug.WriteLine("App Activated");

            /// Close App
            if (eventArgs.Uri.AbsoluteUri.Contains("close"))
            {
                global::Windows.UI.Xaml.Application.Current.Exit();
            }
        }
    }

编辑 1:

正如 Martin Zikmund 所建议的,我尝试做同样的事情,但 javascript 会引发错误。

我认为 AppCommunicator 没有注册。

点击以下链接可能有助于使其正常工作

传递给 AddWebAllowedObject 的对象必须从一个 独立于应用程序集的 Windows 运行时组件。这 对 AllowForWeb 属性进行属性标识是必需的 由 WebView 安全子系统。如果您使用应用中的课程 项目,AddWebAllowedObject 不起作用。

更多详情请参考WebAllowedObject

【问题讨论】:

    标签: uwp uwp-xaml windows-applications


    【解决方案1】:

    问题在于WebView 控件运行Edge 的嵌入式版本,“技术上”它不是同一个应用程序并且会导致弹出窗口。

    更好的解决方案是直接从 JS 调用 C# 代码。您可以使用 Web 允许的对象来做到这一点。这是一个特殊的 Windows 运行时类的实例,将被允许进入 JS,您将能够使用该实例进行通信。因为首先您需要创建一个新的 Windows 运行时组件项目。在那里你定义了一个带有 [AllowForWeb] 属性的类,然后可以从 JS 调用它:

    [AllowForWeb] 
    public sealed class AppCommunicator
    {
        public void CloseApp()
        {
            Application.Current.Exit();
        }
    }
    

    现在从您的主 UWP 应用项目中添加对这个新项目的引用,然后您需要将该类的实例注入网页:

    private void MainWebView_NavigationStarting(WebView sender, 
                    WebViewNavigationStartingEventArgs args)
    { 
       sender.AddWebAllowedObject("AppCommunicator", new AppCommunicator());
    }
    

    现在你可以在 JS 中调用对象的方法了:

    AppCommunicator.CloseApp();
    

    脚本通知

    还有另一种从 JS 通知 C# 代码的方法,即ScriptNotify,但仅支持 HTTPS 网站。欲了解更多信息,请参阅the documentation

    【讨论】:

    • 感谢您的建议,但在同一解决方案中添加 AppCommunicator 无效。 AppCommunicator 在 Javascript 中不可用。根据此链接它必须在单独的项目中。 social.msdn.microsoft.com/Forums/netframework/en-US/…
    • 嗨,我已经用 Windows 运行时组件的信息更新了我的答案
    【解决方案2】:

    我添加了 WebView1_ScriptNotify 方法,该方法在 window.external.notify(param); 时调用从 UI 调用

    // Add in MainPage.xaml.cs
    void WebView1_ScriptNotify(object sender, NotifyEventArgs e)
    {
        if (e.Value != null && e.Value.Contains("close"))
        {
            global::Windows.UI.Xaml.Application.Current.Exit(); // closing App if close is passed as arg
        }
        else
        {
            /// Other stuffs
        }
    }
    

    WebView1_ScriptNotify 在 WebView 中定义为

    ScriptNotify="WebView1_ScriptNotify"
    

    注意:window.external.notify 仅当您通过

    添加 ContentURIs 时才能在 javascript 中访问

    解决方案 => Package.appxmanifest => ContentURIs 选项卡

    示例 Uri:

    http://192.168.0.105:8080
    

    【讨论】:

      猜你喜欢
      • 2023-03-12
      • 2014-02-22
      • 2013-03-13
      • 1970-01-01
      • 2017-03-06
      • 1970-01-01
      • 1970-01-01
      • 2012-04-26
      • 1970-01-01
      相关资源
      最近更新 更多