WPF WebBrowser - 在默认浏览器中打开链接 [英] WPF WebBrowser - open links in default browser

查看:80
本文介绍了WPF WebBrowser - 在默认浏览器中打开链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 WPF System.Windows.Controls.WebBrowser 控件来显示一些从服务下载的 HTML 内容.有时 HTML 包含应该可点击的 URL(a"元素).

I am using a WPF System.Windows.Controls.WebBrowser control to show some HTML content that is downloaded from a service. Sometimes the HTML contains URLs ("a" elements) that should be clickable.

默认情况下,当点击此类 URL 时,它会在 Internet Explorer 中打开.我希望它们改为在默认浏览器中打开.

By default, when such an URL is clicked it opens in Internet Explorer. I want them to open in the default browser instead.

请注意,我是在专门讨论 WPF WebBrowser.WinForms 浏览器有很多解决方案,但它们不适用于 WPF 浏览器.

Note I am talking specifically about the WPF WebBrowser. There are tons of solutions for the WinForms browser, but they do not work for the WPF browser.

最常见的解决方案"是处理 Navigating 事件,取消它,然后用 URL 做你自己的事情.这不起作用,因为当我单击 HTML 中的链接时不会调用 Navigating 事件.

The most common "solution" is to handle the Navigating event, cancel it and then do your own thing with the URL. This does not work because the Navigating event is not called when I click a link in the HTML.

我发现的另一个解决方案似乎确实有效,但只是有时出于某些奇怪的原因:https://stackoverflow.com/a/9111151/573249

Another solution I found does seem to work, but only sometimes for some strange reason: https://stackoverflow.com/a/9111151/573249

我现在有以下代码,使用上面链接中的方法:

I now have the following code, using the method from the link above:

private void WebBrowser_OnNavigating(object sender, NavigatingCancelEventArgs e)
{
    // Just for demonstration purposes
    // This is NOT CALLED when a link is clicked

    Debug.WriteLine("> Navigating called.");

    if (e.Uri == null)
    {
        Debug.WriteLine(">> URI was null.");
        return;
    }
    e.Cancel = true;
    Process.Start(e.Uri.AbsolutePath);
    Debug.WriteLine(">> Navigation cancelled and opened in default browser.");
}

private void WebBrowser_OnLoadCompleted(object sender, NavigationEventArgs e)
{
    Debug.WriteLine("> LoadCompleted called.");

    mshtml.HTMLDocument doc;
    doc = (mshtml.HTMLDocument) webBrowser.Document;
    mshtml.HTMLDocumentEvents2_Event iEvent;
    iEvent = (mshtml.HTMLDocumentEvents2_Event) doc;
    iEvent.onclick += new mshtml.HTMLDocumentEvents2_onclickEventHandler(ClickEventHandler);

    Debug.WriteLine("> LoadCompleted finished!");
    Debug.WriteLine("------");
}

private bool ClickEventHandler(mshtml.IHTMLEventObj e)
{
    // This finally opens the URL in the default browser
    // The method is called only 20% of the time.

    Debug.WriteLine(">> Click event handler called.");

    var a = (mshtml.HTMLAnchorElement) e.srcElement;
    Process.Start(a.href);
    return false;
}

当我点击一个链接时,它似乎有 20% 的时间有效.在这些情况下,将调用ClickEventHandler",并在默认浏览器中打开链接.在其他 80% 的情况下,ClickEventHandler"从未被调用(并且链接在 IE 中打开),即使OnLoadCompleted"无异常完成.

When I click a link, it seems to work maybe 20% of the time. In those cases, "ClickEventHandler" is called, and the link is opened in the default browser. In the other 80% of the cases, "ClickEventHandler" is never called (and the link opens in IE), even though "OnLoadCompleted" finishes without exceptions.

似乎没有模式,虽然当它失败一次时,它似乎在同一个文档上永远失败,直到我重新加载 HTML.重新加载后,它恢复到 20% 的工作机会.

There does not seem to be a pattern, although when it fails once it seems to keep failing forever on the same document until I re-load the HTML. After re-loading it is back to 20% chance of working.

怎么回事?

推荐答案

我遇到了完全相同的问题.OnMouseDown 事件有时会被触发.而我无法弄清楚这一点.我只是放弃并使用 WinForm WebBrowser 代替.那么你不需要 MSHTML 的参考,但你需要 System.Windows.Forms 和 System.Windows.Interactivity 的参考.

Hi I had exactly the same problem. OnMouseDown event was fired just sometimes. And I couldn't figure this out. I just gave up and used WinForm WebBrowser instead. Then you don't need reference for MSHTML but you need reference for System.Windows.Forms and System.Windows.Interactivity.

XAML

xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"

<WindowsFormsHost>
  <wf:WebBrowser x:Name="WbThumbs"/>
</WindowsFormsHost>

C#

public WMain() {
  System.Windows.Forms.Application.EnableVisualStyles();
  InitializeComponent();

  WbThumbs.DocumentCompleted += WbThumbsOnDocumentCompleted;
  WbThumbs.Navigate("www.blabla.com");
}

private void WbThumbsOnDocumentCompleted(object sender, System.Windows.Forms.WebBrowserDocumentCompletedEventArgs e) {
  if (WbThumbs.Document == null) return;
  WbThumbs.Document.Click += WbThumbs_Click;
}

private void WbThumbs_Click(object sender, System.Windows.Forms.HtmlElementEventArgs e) {
  var doc = WbThumbs.Document;
  var src = doc?.GetElementFromPoint(e.ClientMousePosition);
  if (src == null) return;
  //...
}

这篇关于WPF WebBrowser - 在默认浏览器中打开链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆