【问题标题】:WebBrowser-Control - open default browser on click on linkWebBrowser-Control - 点击链接打开默认浏览器
【发布时间】:2012-06-11 06:20:57
【问题描述】:

我在我的 WPF 应用程序中使用 WebBrowser-Control,例如

    <WebBrowser x:Name="webBrowser" Margin="0,28,0,0" />

现在,当我导航到包含链接的 mht 页面并且用户单击此链接之一时,新页面将在 WebBrowser-Control 中打开。但它应该在新的默认浏览器窗口中打开。不应更改 WebBrowser-Control 中的内容。 有没有办法改变这种行为?

【问题讨论】:

    标签: c# .net wpf webbrowser-control


    【解决方案1】:

    默认情况下,Web 浏览器控件在单击链接时不会打开默认浏览器,它仅在 Internet Explorer 中打开在浏览器中单击的链接。现在我们可以使用 _DocumentCompleted 事件,但它需要一个基于事件的触发器,比如链接按钮,才能工作。现在的问题是,如果浏览器控件中的 html 有 href,那么这甚至不起作用。解决方案是使用 _NewWindow 事件。代码如下

    /* The event handler*/
    private void webBrowser1_NewWindow(object sender, CancelEventArgs e)
            {
                var webbrowser = (WebBrowser)sender;
                e.Cancel = true;
                OpenWebsite(webbrowser.StatusText.ToString());
                webbrowser = null;
            }
    
    /* The function call*/
    public static void OpenWebsite(string url)
            {
                Process.Start(GetDefaultBrowserPath(), url);
            }
    
     private static string GetDefaultBrowserPath()
            {
                string key = @"http\shell\open\command";
                RegistryKey registryKey =
                Registry.ClassesRoot.OpenSubKey(key, false);
                return ((string)registryKey.GetValue(null, null)).Split('"')[1];
            }
    

    欢迎提出改进建议。编码愉快。

    【讨论】:

    • 您不需要GetDefaultBrowserPath 代码。使用Process.Start(url)(即没有GetDefaultBrowserPath() arg)更好,因为它要求操作系统打开网页,操作系统自然会使用默认浏览器。然后,您就不需要注册表代码,这可能会破坏前向兼容性。
    • webbrowser 控件没有任何名为 DocumentCompleted 或 NewWindow 的事件。你能也分享一下xaml代码吗?
    【解决方案2】:

    您可以在导航事件中使用Proces.Start() 在默认浏览器中打开新页面,并设置e.Cancel = true; 以便控件中的页面不会改变。

    例子:

    @MainWindow.xaml.cs

    using System.Diagnostics;
    using System.Windows;
    using System.Windows.Navigation;
    
    namespace OpenDefaultBrowser
    {
        public partial class MainWindow : Window
        {
            private static bool willNavigate;
    
            public MainWindow()
            {
                InitializeComponent();
            }
    
            private void webBrowser1_Navigating(object sender, NavigatingCancelEventArgs e)
            {
                // first page needs to be loaded in webBrowser control
                if (!willNavigate)
                {
                    willNavigate = true;
                    return;
                }
    
                // cancel navigation to the clicked link in the webBrowser control
                e.Cancel = true;
    
                var startInfo = new ProcessStartInfo
                {
                    FileName = e.Uri.ToString()
                };
    
                Process.Start(startInfo);
            }
        }
    }
    

    @MainWindow.xaml

    <Window x:Class="OpenDefaultBrowser.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="464" Width="1046">
        <Grid>
            <WebBrowser Height="425" HorizontalAlignment="Left" Name="webBrowser1" VerticalAlignment="Top" Width="1024" Source="http://stackoverflow.com/" Navigating="webBrowser1_Navigating" />
        </Grid>
    </Window>
    

    【讨论】:

    • 更安全的检查可能是 e.Uri == null。非点击加载就是这种情况。
    【解决方案3】:

    我不认为WebBrowser 打算像这样,即使在普通浏览器中,如果单击超链接并且它代表一个简单的 URL(而不是基于 javascript 的超链接),它将在该浏览器窗口中打开 URL(和那个特定的标签)本身。 WebBrowser 控件模仿浏览器本身的这种基本行为。

    我认为您可以右键单击超链接并说“在新窗口中打开”(查看WebBrowser 控件中是否启用了该选项)。

    如果该选项被禁用,您可以使用特殊的 HTMLHost API 来启用它。

    【讨论】:

      猜你喜欢
      • 2011-05-12
      • 1970-01-01
      • 1970-01-01
      • 2014-09-30
      • 2011-07-12
      • 2012-07-18
      • 1970-01-01
      • 2014-03-08
      • 1970-01-01
      相关资源
      最近更新 更多