2015-04-29 121 views
0

這是我的第一篇文章,所以我希望這是正確的地方,請好!謝謝。無法投射物體,

所以我的問題是,我下載文件時出現錯誤。我製作了一個下載器應用程序來下載一個特定的文件。它運行得很好,直到它遇到下載完成事件。

下面是代碼:

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',at Database_Download.frm.Main._Lambda $ __ 1(Object a0。 AsyncCompletedEventArgs a1)

就像我說的所有運行良好,直到它需要觸發DownloadComplete,但它並沒有說我的消息箱,它只是彈出這個錯誤。我不明白這一點,因爲我已經在沒有問題的其他項目中完成了這個確切的代碼。 請幫忙!

回答

0

更改簽名。 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 
+0

這樣做的竅門!這很奇怪,就像我說過的,這在其他項目中起作用。我應該在其他/未來的項目中更改它嗎? –

+0

只要確保它們匹配。當您將鼠標懸停在「Addhandler」聲明中的事件上時,您應該能夠看到簽名。 – OneFineDay

+0

好的,再次感謝! –