.NET WebBrowser控件和刷新

时间:2013-03-31 07:19:47

标签: .net winforms webbrowser-control

我有一个简单的WinForm,其上有一个WebBrowser控件,可以显示来自Web的图像。根据Timer1刷新图像 它工作正常,直到我按下 F5 或从浏览器的上下文菜单中使用'刷新'选项。然后我得到一个空白页面,我必须重新启动该程序 那是为什么?
我希望能够在不需要等待Timer1的情况下手动更新 有什么建议吗? 我使用的是Visual Basic Express 2010。

 Public Class Form1
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        WebBrowser1.DocumentText = "<html><body><img src='http://example.com/image.jpg'></body></html>"
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        WebBrowser1.Refresh()
    End Sub
End Class

1 个答案:

答案 0 :(得分:2)

写入DocumentText不会更改Url,并且会在刷新时重新获取Url,因此当您按F5时,浏览器会刷新about:blank。我真的很想知道.Refresh()为你工作。

你想要这样的东西:

Private Shared ReadOnly about_blank As Uri = New Uri("about:blank")

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    WebBrowser1.Url = about_blank
End Sub

Private Sub WebBrowser1_Navigated(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserNavigatedEventArgs) Handles WebBrowser1.Navigated
    If e.Url = about_blank Then
        WebBrowser1.Document.Write("<html><body><img src='http://example.com/image.jpg'></body></html>")
    End If
End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    WebBrowser1.Refresh()
End Sub