简体   繁体   English

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

[英]Why is WebBrowser_DocumentCompleted() firing twice?

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. 好吧,我使用一个简单的webbrowser控件来浏览页面,所以我需要在这样做时更改表单的Text。 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. 我甚至尝试过_Navigated()事件。 it also fired twice. 它也开了两次。 Resulting the title to "Webber - google.co.in - google.co.in" .. 得到的标题为"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.. 我也注意到这个事件在加载msn.com时多次激活..我只是在页面完成加载时才尝试更改表单的文本..

Any remedy? 任何补救措施?

You can check the WebBrowser.ReadyState when the event is fired: 您可以在触发事件时检查WebBrowser.ReadyState:

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

ReadyState will be set to Complete once the whole document is ready. 整个文档准备就绪后,ReadyState将设置为Complete。

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. 此外,在您去那里之前, IsBusy属性将仅在第一帧未加载时为True

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

  //The page is finished loading 
}

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
    }

它每帧被触发一次。

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 它将加载两个不同的页面: 关于:BlankNewUrl.com

Just, remove the designer code... and you'll stop the "double" event. 只是,删除设计师代码......然后你就会停止“双重”事件。

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. 当您每次订阅此活动时导航到URL时,您可能会多次订阅此活动,就像您使用某种方法一样。

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 . 在类的构造函数中......也许应该解决你的问题。

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. 如何确定页面何时完成加载WebBrowser控件 DocumentCompleted是WinForms的DocumentComplete外翻包装器,但WebBrowserDocumentCompletedEventArgs隐藏了sender参数,因此您无法分辨哪个帧正在引发事件。 Alternatively you can check WebBrowser.ReadyState . 或者,您可以检查WebBrowser.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. 我有一个计时器,只是反复检查ReadyState几分钟。 (Using embedded browser control). (使用嵌入式浏览器控件)。

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

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. 当页面中有框架时,DocumentCompleted将被多次触发。这很难解决。检查网址的一些方法并不准确。

BTW, why not using this: 顺便说一句,为什么不用这个:

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

Try to using a fixed prefix,problem may be solved easily. 尝试使用固定前缀,问题可以轻松解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM