简体   繁体   English

无法转换类型的对象,

[英]Unable to cast object of type,

This is my first post so I hope this is the right place and please be nice! 这是我的第一篇文章,所以我希望这是正确的地方,请保持友好! Thanks. 谢谢。

So my issue is that I get an error while downloading a file. 所以我的问题是下载文件时出现错误。 I have made a downloader app to download a specific file. 我制作了一个下载器应用程序来下载特定文件。 It runs just fine until it hits the download complete event. 它运行良好,直到遇到下载完成事件。

Here is the code: 这是代码:

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

The error I get is an 我得到的错误是

Unhandled Exception: System.InvalidCastException: Unable to cast object to type 'System.ComponentModel.AsyncCompletedEventArgs' to type 'System.Net DownloadDataCompletedEventArgs', at Database_Download.frm.Main._Lambda$__1(Object a0. AsyncCompletedEventArgs a1) 未处理的异常:System.InvalidCastException:无法将对象转换为类型为“ System.ComponentModel.AsyncCompletedEventArgs”的类型为“ System.Net DownloadDataCompletedEventArgs”,位于Database_Download.frm.Main._Lambda $ __ 1(对象a0。AsyncCompletedEventArgs a1)

Like I said all runs fine until it needs to fire the DownloadComplete , but it does not say my messagebox at all it just pops this error. 就像我说的那样,在需要启动DownloadComplete之前,一切运行良好,但它并没有说我的消息框,它只是弹出此错误。 I don't understand this because I have done this exact code in other projects without problems. 我不明白这一点,因为我已经在其他项目中完成了这些准确的代码而没有出现问题。 Please help! 请帮忙!

Change the signature. 更改签名。 MSDN documentation MSDN文档

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM