2008-11-10 90 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. 
相關問題