为什么 WebBrowser_DocumentCompleted() 会触发两次?

发布于 2024-08-23 00:53:14 字数 454 浏览 1 评论 0原文

嗯,我正在使用一个简单的网络浏览器控件来浏览页面,因此我需要在执行此操作时更改表单的文本。我正在使用 -

private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
     this.Text += " - " + webBrowser1.Document.Domain;
}

但使用断点,我注意到,这个事件触发了两次。我什至尝试了 _Naviged() 事件。它还发射了两次。结果标题为 “Webber - google.co.in - google.co.in” ..

我还注意到加载 msn.com 时此事件触发了几次..我正在尝试更改仅当页面完全加载完成时才显示表单文本。

有什么补救措施吗?

Well, I'm using a simple webbrowser control to browse to a page, so I need to change the Text of the form while doing so. I'm using -

private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
     this.Text += " - " + webBrowser1.Document.Domain;
}

but using a breakpoint, i noticed that, this event is firing twice. I even tried _Navigated() event. it also fired twice. Resulting the title to "Webber - google.co.in - google.co.in" ..

I also noticed that this event fired several times while loading msn.com.. I'm trying to change the text of the form only when the page has finished loading totally..

Any remedy?

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

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

发布评论

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

评论(9

音栖息无 2024-08-30 00:53:14

您可以在事件触发时检查 WebBrowser.ReadyState:

if (browser.ReadyState != WebBrowserReadyState.Complete)
    return;

一旦整个文档准备就绪,ReadyState 将设置为 Complete。

You can check the WebBrowser.ReadyState when the event is fired:

if (browser.ReadyState != WebBrowserReadyState.Complete)
    return;

ReadyState will be set to Complete once the whole document is ready.

凉宸 2024-08-30 00:53:14

每次加载一帧时,都会触发该事件。

另外,在您到达那里之前,当第一帧尚未加载时,IsBusy 属性只会为 True

void BrowserDocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
  if (e.Url.AbsolutePath != (sender as WebBrowser).Url.AbsolutePath)
    return; 

  //The page is finished loading 
}

Every time a frame loads, the event is fired.

Also, before you even go there, the IsBusy property will only be True whilst the first frame has not loaded.

void BrowserDocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
  if (e.Url.AbsolutePath != (sender as WebBrowser).Url.AbsolutePath)
    return; 

  //The page is finished loading 
}
三生一梦 2024-08-30 00:53:14

我有同样的问题,原因是因为默认情况下,当您添加控件时,它会生成这样的设计器代码。

this.webBrowser1.Url =  new System.Uri("", System.UriKind.Relative);

如果您在调用后更改网址,

InitializeComponent();
WebBrowser.Navigate("NewUrl.com");

它将加载两个不同的页面:About:BlankNewUrl.com

只需删除设计器代码...您就会停止“双”事件。

I have the same problem, and the reason was because, by default when you add the control it generate designer code like this.

this.webBrowser1.Url =  new System.Uri("", System.UriKind.Relative);

and if you change the url after calling

InitializeComponent();
WebBrowser.Navigate("NewUrl.com");

It will load two different pages: About:Blank and NewUrl.com

Just, remove the designer code... and you'll stop the "double" event.

2024-08-30 00:53:14

它每帧被触发一次。

It gets fired once per frame.

ㄖ落Θ余辉 2024-08-30 00:53:14

如果发射两次是一个问题,那么这应该有效:

  string body="";

    private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        if (body == webBrowser1.Document.Body.InnerHtml) return;
        body = webBrowser1.Document.Body.InnerHtml;

        // Here is something you want
    }

If firing twice is a problem then this should work:

  string body="";

    private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        if (body == webBrowser1.Document.Body.InnerHtml) return;
        body = webBrowser1.Document.Body.InnerHtml;

        // Here is something you want
    }
南渊 2024-08-30 00:53:14

可能您会多次订阅此事件,就像在每次订阅此事件时导航到 URL 的某些方法中一样。

要解决此问题,请将该行移出方法并将其放置在每个实例仅调用一次的其他位置。在类的构造函数中也许......这应该可以解决你的问题。

Might be you are subscribing this event multiple times like in your some method when your navigating to URL everytime you subscribe to this event.

To solve this problem, move that line out of the method and put it somewhere else where it will only be called once per instance. In the constructor of the class perhaps... That should solve your problem .

丑疤怪 2024-08-30 00:53:14

事实上,它并不总是被解雇。还没想明白为什么不可以。我有一个计时器,只需重复检查 ReadyState 几分钟即可。 (使用嵌入式浏览器控件)。

Actually, it doesn't always get fired. Haven't figured out why not. I have a timer and just check the ReadyState repeatedly for a few minutes. (Using embedded browser control).

寒江雪… 2024-08-30 00:53:14

建议使用 if (browser.ReadyState != WebBrowserReadyState.Complete)

而且当页面中有框架时,DocumentCompleted会被触发多次。而且这个问题很难解决。有些方法比如检查url并不准确。

顺便说一句,为什么不使用这个:

this.Text = stringA + " - " + webBrowser1.Document.Domain;

尝试使用固定前缀,问题可能会很容易解决。

if (browser.ReadyState != WebBrowserReadyState.Complete) is recommended.

And when there are frames in the page,DocumentCompleted will be fired several times.And this is difficult to solve.Some ways like checking the urls are not accurate.

BTW, why not using this:

this.Text = stringA + " - " + webBrowser1.Document.Domain;

Try to using a fixed prefix,problem may be solved easily.

筑梦 2024-08-30 00:53:14

如何确定 WebBrowser 控件中页面加载完成的时间
DocumentCompleted 是 WinForms 的 DocumentComplete 事件的包装器,但是 WebBrowserDocumentCompletedEventArgs 隐藏了发送者参数,因此您无法判断哪个框架正在引发该事件。
或者,您可以检查WebBrowser.ReadyState

How To Determine When a Page Is Done Loading in WebBrowser Control
DocumentCompleted is WinForms' wrapper of the DocumentComplete evert, however WebBrowserDocumentCompletedEventArgs hides the sender parameter so you cannot tell which frame is raising the event.
Alternatively you can check WebBrowser.ReadyState.

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