【问题标题】:xaml WebView: How to catch a "OnNavigation" event @Windows Store Appxaml WebView:如何捕捉“OnNavigation”事件@Windows Store App
【发布时间】:2013-03-27 07:56:15
【问题描述】:

我正在使用 C#/xaml 开发 Windows 应用商店应用程序。

如果我的 WebView 中的链接被点击,我如何注意到?该 afaik 没有事件。我已经研究过thisthat,但它没有为我提供合适的解决方案。

我可以举个例子:我通过 iFrame 将 Facebook 页面的流嵌入到我的 WebView 中。我想阻止所有可能在该 iFrame 中的链接。

【问题讨论】:

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


    【解决方案1】:

    您可以使用您自己的一些 Javascript 调用 InvokeScript,以设置一个侦听器,以便在用户离开您的页面时进行监听。这在 C# 中类似于以下内容:

    var navigationListenerString = @"
    (function() {
      function leavingPage() {
        window.external.notify("LEAVING PAGE");
      }
      window.onbeforeunload = leavingPage;
    })()";
    
    webView.InvokeScript("eval", new string[] { navigationListenerString });
    

    然后您可以使用 ScriptNotify 来侦听您的特定消息,以确定页面正在卸载并且用户正在离开。不幸的是,您无法检测到用户的去向。此外,如果超链接在新窗口中打开并且 web 视图未卸载,您也无法检测到。

    【讨论】:

    • 我在尝试此操作时遇到 SystemException:DISP_E_UNKNOWNNAME
    • 经过一番折腾,我找到了解决方案:您可能做得太早了。您应该仅在加载页面后执行此操作。确保在 TestWebView_LoadCompleted(...) 或更高版本中使用它。另请注意,网页也可以发送 LEAVING PAGE,因此可能会生成随机密钥或其他东西。另外,请注意,您需要将网页(或使用 AnyScriptNotifyUri 的所有网页)添加到 allowedscriptnotifyuris 列表。 msdn.microsoft.com/en-us/library/windows/apps/…
    猜你喜欢
    • 2017-07-27
    • 2014-07-27
    • 1970-01-01
    • 1970-01-01
    • 2013-10-28
    • 1970-01-01
    • 1970-01-01
    • 2018-10-13
    • 2019-03-01
    相关资源
    最近更新 更多