为什么 WebBrowser_DocumentCompleted() 触发两次? [英] Why is WebBrowser_DocumentCompleted() firing twice?

查看:23
本文介绍了为什么 WebBrowser_DocumentCompleted() 触发两次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗯,我正在使用一个简单的 webbrowser 控件来浏览页面,所以我需要在这样做时更改表单的文本.我正在使用 -

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;
}

但是使用断点,我注意到,这个事件触发了两次.我什至尝试过 _Navigated() 事件.它也发射了两次.结果标题为 "Webber - google.co.in - google.co.in" ..

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" ..

我还注意到此事件在加载 msn.com 时触发了多次.. 我试图仅在页面完全加载完成后才更改表单的文本..

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..

有什么补救措施吗?

推荐答案

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

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

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

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

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

这篇关于为什么 WebBrowser_DocumentCompleted() 触发两次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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