为什么 WebBrowser 控件有这种奇怪的行为?

发布于 2025-01-01 06:09:27 字数 608 浏览 2 评论 0原文

我有一个带有 webbrowser 的表单。事件:NavigatingNavigedDocumentCompleted 已附加。

这就是我告诉它后得到的结果 .Navigate(new Url("http://google.com"));

20:42:42:036: 1. Navigating to: http://google.com/
20:42:42:545: 2. Navigated: http://www.google.com/
20:42:42:854: 3. Navigating to: about:blank
20:42:43:002: 4. Navigated: about:blank
20:42:43:004: 5. Loaded: about:blank
20:42:43:158: 6. Loaded: http://www.google.com/

有人能解释一下为什么我得到这个 about:blank东西?

该代码仅显示每个事件处理程序的 e.Url 参数。

I have a form with a webbrowser. Events: Navigating, Navigated and DocumentCompleted are attached.

This is what I am getting after telling it .Navigate(new Url("http://google.com"));

20:42:42:036: 1. Navigating to: http://google.com/
20:42:42:545: 2. Navigated: http://www.google.com/
20:42:42:854: 3. Navigating to: about:blank
20:42:43:002: 4. Navigated: about:blank
20:42:43:004: 5. Loaded: about:blank
20:42:43:158: 6. Loaded: http://www.google.com/

Can somebody explain why I am getting this about:blank stuff?

The code is just displaying the e.Url parameter of each event handler.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

把昨日还给我 2025-01-08 06:09:27

似乎自动导航到 about:blank 是一种已知的“安全毯”,由 WebBrowser 对象使用,以确保 HTML 对象在尝试加载您请求的页面之前有效,如中所述这篇 MSDN 文章

[...] IWebBrowser2::Navigate2 方法用于导航到 about:blank 页面。导航到此空页面可确保加载 MSHTML 并且可通过动态 HTML (DHTML) 对象模型使用 HTML 元素。

此外,DocumentCompleted 事件应该可以工作。您确定您的活动没有做任何其他事情吗?

我已经尝试过这个简单的代码,它按预期工作:

namespace CSharpWindowsPractice
{
    using System;
    using System.Windows.Forms;

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
        {
            this.listBox1.Items.Add("Navigated to: " + e.Url);
        }

        private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
        {
            this.listBox1.Items.Add("Navigating to: " + e.Url);
        }

        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            this.listBox1.Items.Add("DocumentCompleted: " + e.Url);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.webBrowser1.Navigate(new Uri(@"http://google.com"));
        }
    }
}

按下按钮后,您会得到以下内容:

It appears that the automatic navigation to about:blank is a known "security blanket" of sorts used by the WebBrowser object to ensure the HTML object is valid before trying to load the page you request, as explained in this MSDN article:

[...] the IWebBrowser2::Navigate2 method is used to navigate to the about:blank page. Navigating to this empty page ensures that MSHTML is loaded and that the HTML elements are available through the Dynamic HTML (DHTML) Object Model.

Also, the DocumentCompleted event should work. Are you sure your events aren't doing anything else?

I've tried this simple code, and it works as expected:

namespace CSharpWindowsPractice
{
    using System;
    using System.Windows.Forms;

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
        {
            this.listBox1.Items.Add("Navigated to: " + e.Url);
        }

        private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
        {
            this.listBox1.Items.Add("Navigating to: " + e.Url);
        }

        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            this.listBox1.Items.Add("DocumentCompleted: " + e.Url);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.webBrowser1.Navigate(new Uri(@"http://google.com"));
        }
    }
}

After pushing the button, you get the following:

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文