可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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?
回答1:
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.
回答2:
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.
Try this:
void BrowserDocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
if (e.Url.AbsolutePath != (sender as WebBrowser).Url.AbsolutePath)
return;
//The page is finished loading
}
回答3:
It gets fired once per frame.
回答4:
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.
回答5:
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
}
回答6:
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 .
回答7:
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).
回答8:
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.
回答9:
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
.