代码之家  ›  专栏  ›  技术社区  ›  wusher

如何使用.NET2.0中的WebBrowser控件检查ajax更新?

  •  4
  • wusher  · 技术社区  · 17 年前

    我有一个网页正在使用WebBrowser控件在winform应用程序中显示。当网页中的HTML更改时,我需要执行一个事件;但是,我找不到通过Ajax更新页面时触发的事件。DocumentComplete、FileDownloaded和ProgressChanged事件并不总是由Ajax请求触发。我能想到的解决问题的唯一其他方法是轮询文档对象并查找更改;然而,我认为这不是一个很好的解决方案。

    有没有我错过的另一个事件会在ajax更新或其他解决问题的方法中触发?

    我正在使用C#和.NET2.0

    1 回复  |  直到 17 年前
        1
  •  3
  •   Patrick    17 年前

    我一直在使用一个计时器,只是在一个特定的内容元素的变化观察。

    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
    

    我的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.
    
    推荐文章