【问题标题】:How to do something once webbrowser document has loaded加载 webbrowser 文档后如何做某事
【发布时间】:2013-09-09 17:03:01
【问题描述】:

只有在 vb.net 网络浏览器中完全加载特定 URL 时,我才能在子例程中运行一些东西。

例如

sub button click

webbrowsernavigate to whatever

(This is what I need)if document has loaded statement
do stuff

end sub

谢谢

【问题讨论】:

  • 您在寻找vba 还是vb.net
  • vb.net 请。谢谢
  • 如果目标是 .NET 4.5,您可以使用 async/await。我没有准备好 VB.NET 示例,但 here's how 可以在 C# 中完成。

标签: .net vb.net webbrowser-control


【解决方案1】:

WebBrowser 类有一个可以绑定到的 DocumentCompleted 事件:

WebBrowser 控件完成加载文档时发生。

MSDN 文章中有一个示例演示了如何有效地使用此事件:

Private Sub PrintHelpPage()

    ' Create a WebBrowser instance.  
    Dim webBrowserForPrinting As New WebBrowser()

    ' Add an event handler that prints the document after it loads. 
    AddHandler webBrowserForPrinting.DocumentCompleted, New _
        WebBrowserDocumentCompletedEventHandler(AddressOf PrintDocument)

    ' Set the Url property to load the document.
    webBrowserForPrinting.Url = New Uri("\\myshare\help.html")

End Sub 

Private Sub PrintDocument(ByVal sender As Object, _
    ByVal e As WebBrowserDocumentCompletedEventArgs)

    Dim webBrowserForPrinting As WebBrowser = CType(sender, WebBrowser)

    ' Print the document now that it is fully loaded.
    webBrowserForPrinting.Print()
    MessageBox.Show("print")

    ' Dispose the WebBrowser now that the task is complete. 
    webBrowserForPrinting.Dispose()

End Sub

【讨论】:

  • 嗨,有没有办法以与我问题中的代码类似的方式做到这一点?
  • @BenHoldenCrowther 可能,但我不会推荐它。 WebBrowser 控件是异步的,因此在加载时不会阻塞 UI。使用这样的事件是一种非常更优雅的处理方式,它只是在您等待文档下载时阻塞整个应用程序。
  • 您知道一种与我的问题类似的方法吗?我真的很感激。
  • webbbrowser 控件从后台线程引发 BeforeNavigate2 和其他事件。如果这些事件被您的等待代码阻止,导航将不会发生。
猜你喜欢
  • 1970-01-01
  • 2015-07-31
  • 1970-01-01
  • 2013-06-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-17
  • 1970-01-01
相关资源
最近更新 更多