【问题标题】:How do you check for ajax updates using a WebBrowser control in .net 2.0?如何使用 .net 2.0 中的 WebBrowser 控件检查 ajax 更新?
【发布时间】:2009-02-17 22:30:49
【问题描述】:

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

是否还有另一个我错过的事件将在 ajax 更新或其他解决问题的方式上触发?

我正在使用 C# 和 .net 2.0

【问题讨论】:

    标签: c# .net ajax winforms


    【解决方案1】:

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

    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 事件和 Timer 上的 Tick 事件。

    我为每个所需的操作编写 DocumentComplete 或 Tick 处理程序,每个处理程序通常都会自己删除处理程序,因此成功的事件只会被处理一次。我还有一个名为 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.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-26
      • 1970-01-01
      • 2015-05-07
      • 1970-01-01
      • 2013-09-09
      • 1970-01-01
      • 2010-12-02
      • 1970-01-01
      相关资源
      最近更新 更多