WebView:如何捕捉OnNavigation"@Windows Store应用程序

本文关键字:@Windows Store 应用程序 quot 何捕捉 OnNavigation WebView | 更新日期: 2023-09-27 18:06:17

我正在用c#/xaml开发Windows Store应用程序。

我如何注意到,如果一个链接在我的WebView被点击?没有事件可以解决这个问题。我已经研究了这研究了那研究,但是没有给我一个合适的解决方案。

我可以给一个例子:我通过iFrame在我的WebView中嵌入一个facebook页面的流。我想阻止所有可能在iFrame中的链接

WebView:如何捕捉OnNavigation"@Windows Store应用程序

您可以使用一些自己的Javascript调用InvokeScript,以便在用户从您的页面导航离开时设置侦听器。这在c#中看起来像下面这样:

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

然后您可以使用ScriptNotify来监听您的特定消息,以确定页面正在卸载并且用户正在离开。不幸的是,您无法检测到用户要去哪里。另外,如果超链接在新窗口中打开,而webview没有卸载,你也无法检测到。