2008-11-10 88 views
3

我有一个网页,使用WebBrowser控件在Winform应用程序中显示。当网页中的HTML发生变化时,我需要执行一个事件;然而,当页面通过Ajax更新时,我无法找到触发事件的情况。 DocumentComplete,FileDownloaded和ProgressChanged事件并不总是由Ajax请求触发的。我认为解决该问题的唯一方法是轮询文档对象并查找更改;不过,我认为这不是一个很好的解决方案。如何在.net 2.0中使用WebBrowser控件检查ajax更新?

是否有另一个事件我缺少,将触发ajax更新或其他方式来解决问题?

我使用C#和.NET 2.0

回答

2

我一直在使用一个计时器,只是看在特定元素含量的变化。

Private AJAXTimer As New Timer 

Private Sub WaitHandler1(ByVal sender As Object, ByVal e As System.EventArgs) 
    'Confirm that your AJAX operation has completed. 
    Dim ProgressBar = Browser1.Document.All("progressBar") 
    If ProgressBar Is Nothing Then Exit Sub 

    If ProgressBar.Style.ToLower.Contains("display: none") Then 
     'Stop listening for ticks 
     AJAXTimer.Stop() 

     'Clear the handler for the tick event so you can reuse the timer. 
     RemoveHandler AJAXTimer.Tick, AddressOf CoveragesWait 

     'Do what you need to do to the page here... 

     'If you will wait for another AJAX event, then set a 
     'new handler for your Timer. If you are navigating the 
     'page, add a handler to WebBrowser.DocumentComplete 
    End If 
Exit Sub 

Private Function InvokeMember(ByVal FieldName As String, ByVal methodName As String) As Boolean 
     Dim Field = Browser1.Document.GetElementById(FieldName) 
     If Field Is Nothing Then Return False 

     Field.InvokeMember(methodName) 

     Return True 
    End Function 

我有2个对象获得事件处理程序,WebBrowser和Timer。 我主要依赖WebBrowser上的DocumentComplete事件和定时器上的Tick事件。

我要求每个操作都写DocumentComplete或Tick处理程序,每个处理程序通常都是RemoveHandler本身,所以一个成功的事件只能处理一次。我还有一个名为RemoveHandlers的过程,它将从浏览器和计时器中删除所有处理程序。

我的AJAX命令通常是这样的:

AddHandler AJAXTimer.Tick, AddressOf WaitHandler1 
InvokeMember("ContinueButton", "click") 
AJAXTimer.Start 

我的导航命令,如:

AddHandler Browser1.DocumentComplete, AddressOf AddSocialDocComp 
Browser1.Navigate(NextURL) 'or InvokeMember("ControlName", "click") if working on a form. 
相关问题