我在VB.Net中使用Cefsharp创建浏览器,我一直在尝试创建一个下载窗口,显示当前下载的进度。我不知道我做错了什么或是否是CEF的工作方式,但我通过将browser.DownloadHandler = New DownloadHandler
添加到Form1_Load
并创建一个像这样的新类来放入下载处理程序( downloading
是我为展示进度而创建的表格:
Public Class DownloadHandler
Implements IDownloadHandler
Public Function OnBeforeDownload(downloadItem As DownloadItem, ByRef downloadPath As String, ByRef showDialog As Boolean) As Boolean Implements IDownloadHandler.OnBeforeDownload
downloadPath = downloadItem.SuggestedFileName
showDialog = True
downloading.Show()
Return True
End Function
Public Function OnDownloadUpdated1(downloadItem As DownloadItem) As Boolean Implements IDownloadHandler.OnDownloadUpdated
My.Settings.downloadpercent = downloadItem.PercentComplete.ToString
Return False
End Function
End Class
在downloading
表单上,我有用于显示进度的代码:
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Dim percentcomplete As Integer = My.Settings.downloadpercent * 5
Me.PictureBox1.Size = New Size(percentcomplete, 25)
End Sub
Private Sub downloading_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Timer1.Start()
End Sub
这可能不是显示ProgressBar
的最佳方式,但我有一个只有绿色条的图片框,表单的总宽度为500px。代码告诉它阅读放入PercentComplete
的{{1}}设置,将其乘以5,因此当进度为100%时,它将覆盖整个表单。
问题是ProgressBar没有更新以显示当前进度。它有一点点,但后来就停止了。我做错了什么,或者My.Settings.downloadpercent
不是一个好的地方吗?有关如何解决此问题的任何建议吗?
编辑:
我正在使用OnDownloadUpdated
。此外,当保存文件对话框出现时,无论您单击保存还是取消,浏览器始终会触发CefSharp 39.0.0-pre03
,因此它会加载我制作的自定义HTML错误页面,因为它需要一个URL来加载HTML ,我输入" http://rendering/" ...所以我猜这将是一个域名更改。这个问题(在评论中)可能是问题,但我们还需要找出它触发LoadError
的原因。
答案 0 :(得分:0)
面对相同或类似问题的人可能会感兴趣的简单解决方案:
在模块中声明Public
您的Picturebox
:
Module Module1
Public PictureBox1 As New PictureBox
End Module
在MainForm
中,您可以声明所需的属性(在InitializeComponent之后)。例如:
With PictureBox1
.BorderStyle = Border3DStyle.Flat
.Image = Bitmap.FromFile(YourImageFileName)
'......
End With
在DownloadHandler
班级:
Public Function OnDownloadUpdated1(downloadItem As DownloadItem) As Boolean
Implements IDownloadHandler.OnDownloadUpdated
Dim percentcomplete As Integer=downloadItem.PercentComplete * 5
PictureBox1.Size = New Size(percentcomplete, 25)
Return False
End Function