【问题标题】:Unable to cast object of type,无法转换类型的对象,
【发布时间】:2015-04-29 00:17:58
【问题描述】:

这是我的第一篇文章,所以我希望这是正确的地方,请多多关照!谢谢。

所以我的问题是下载文件时出错。我制作了一个下载器应用程序来下载特定文件。它运行得很好,直到它遇到下载完成事件。

代码如下:

Private Sub cmdDownload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDownload.Click


    Try
        Dim webClient As New WebClient()
        AddHandler webClient.DownloadProgressChanged, AddressOf client_ProgressChanged
        AddHandler webClient.DownloadFileCompleted, AddressOf client_DownloadFileCompleted
        webClient.DownloadFileAsync(New Uri("https://dl.dropbox.com/s/wtdagv0xvliwvv2/viruslist.dat?dl=0"), Application.StartupPath + "\viruslist.dat")
    Catch ex As Exception

    End Try
End Sub

Public Sub client_ProgressChanged(ByVal sender As Object, ByVal e As System.Net.DownloadProgressChangedEventArgs)
    Try
        Dim bytesIn As Double = Double.Parse(e.BytesReceived.ToString())
        Dim totalBytes As Double = Double.Parse(e.TotalBytesToReceive.ToString())
        Dim percentage As Double = bytesIn / totalBytes * 100
        ProgBar.Value = Integer.Parse(Math.Truncate(percentage).ToString())
        Label3.Text = "Total Complete " + ProgBar.Value.ToString + " %"
    Catch ex As Exception

    End Try
End Sub

Public Sub client_DownloadFileCompleted(ByVal sender As Object, ByVal e As System.Net.DownloadDataCompletedEventArgs)
    MessageBox.Show("Database has finished downloading!", "Download Finished...", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End Sub

我得到的错误是一个

未处理的异常:System.InvalidCastException:无法转换 要键入的对象“System.ComponentModel.AsyncCompletedEventArgs”要键入 'System.Net DownloadDataCompletedEventArgs',在 Database_Download.frm.Main._Lambda$__1(对象 a0. AsyncCompletedEventArgs a1)

就像我说的一切都运行良好,直到它需要触发DownloadComplete,但它根本没有说我的消息框它只是弹出这个错误。我不明白这一点,因为我已经在其他项目中完成了这个确切的代码而没有问题。 请帮忙!

【问题讨论】:

    标签: vb.net


    【解决方案1】:

    更改签名。 MSDN documentation

    Public Sub client_DownloadFileCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs)
       MessageBox.Show("Database has finished downloading!", "Download Finished...", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
    End Sub
    

    【讨论】:

    • 成功了!这很奇怪,就像我说这在其他项目中有效。我应该在其他/未来的项目中将其更改为这个吗?
    • 只要确保它们匹配即可。当您将鼠标悬停在 Addhandler 语句中的事件上时,您应该能够看到签名。
    猜你喜欢
    • 1970-01-01
    • 2014-06-09
    • 2017-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多