【问题标题】:Windows 8 app WebView catch event target="_blank"Windows 8 应用 WebView 捕获事件 target="_blank"
【发布时间】:2014-05-24 16:32:08
【问题描述】:
我想在 Windows 应用程序上创建一个 webview。
问题是我不知道如何在使用 IE 打开窗口时捕获事件。当链接有 target="_blank" 时,windows 会自动打开 IE。我尝试使用 C# 和 JS 制作应用程序,但问题是一样的。
在 JS 部分,我尝试使用“newwindow”添加一个事件,但它不起作用
对于 C# 部分,我在 XAML 上使用了 webview,而对于 HTML,我使用了 x-ms-window
某些链接的问题是它们的标签中有 target="_blank" 。
他们是解决这个问题的方法吗?他们是捕捉这些事件的一种方式吗?
【问题讨论】:
标签:
c#
windows
windows-8
webview
windows-8.1
【解决方案1】:
最简单的解决方案是(使用 jQuery):
$('a[target=_blank]').on('click',function(e){
e.stopPropagation();
e.preventDefault();
window.external.notify($(this).prop('href'));
});
在 Windows 8 中,您必须将页面(主机)添加到 AllowedScriptNotifyUris
在 Windows 8.1 中,您可能需要其他一些解决方法才能使用 window.external.notify,因为允许的主机嵌入在应用清单中并且必须通过 https 加载
【解决方案2】:
在 Windows 10 上启动,可以使用 WebView.NewWindowRequested 事件:
private void WebView1_NewWindowRequested(
WebView sender,
WebViewNewWindowRequestedEventArgs args)
{
// Prevent the browser from being launched.
args.Handled = true;
// Open the Uri in the current window.
sender.Source = args.Uri;
}