【问题标题】:Sending event arg to custom event handler function将事件 arg 发送到自定义事件处理函数
【发布时间】:2020-09-22 01:59:34
【问题描述】:

我有一个启动 webclient 对象的类,并有一个 DownloadProgressChanged 和一个 DownloadFileCompleted 事件。一个表单可以启动这个对象和下载。如果用户选择关闭表单,表单也可以停止下载。

DownloadFileCompleted 事件处理函数还接受一个参数文件名来引发事件,然后表单类可以处理该事件。我阅读了如何定义此类自定义处理程序函数,因此我的代码如下所示:

AddHandler fileReader.DownloadFileCompleted, Sub(sender, e) download_complete(FileName)
AddHandler fileReader.DownloadProgressChanged, AddressOf Download_ProgressChanged
fileReader.DownloadFileAsync(New Uri(Address), ParentPath + FileName)

Private Sub download_complete(filename As String)
        RaiseEvent DownloadDone(filename)
        
End Sub

我注意到当用户关闭表单时,下载会停止,但我仍然会收到一个下载完成事件(我已经读到这将与 e.Cancelled = True 一起出现)

我的问题是我想在引发事件时发送此信息,所以我的download_complete Sub 会显示如下内容:

Private Sub download_complete(filename As String, e as AsyncCompletedEventArgs)

        If e.Cancelled = True Then
            RaiseEvent DownloadDone(filename, "CANCELLED")

        Else
            RaiseEvent DownloadDone(filename, "COMPLETE")
        End If
        
End Sub

这样我就可以在表单的事件处理方法中很好的处理下面的流程了。我找不到该方法的任何文档。有人可以帮帮我吗?

【问题讨论】:

  • 事件处理程序应该始终只有两个参数:senderObject 类型,应该是引发事件的对象,e 应该是 EventArgs 类型或派生自它,并且包含事件的所有数据。如果要将文件路径传递给事件处理程序,那么它应该作为 e 参数的属性。我建议你阅读this
  • 感谢您的回复@jmcilhinney!我浏览了您的博客,据我了解,我需要创建一个从 EventArgs 类继承的自定义事件,然后我可以将一些数据添加到新的事件类中,该类可以在引发事件时使用。但是在我的情况下,我需要使用 DownloadFileCompleted 事件,我认为我不能手动向该事件添加任何数据。

标签: vb.net event-handling webclient


【解决方案1】:

如果我对您的理解正确,那么您需要这样做:

Imports System.ComponentModel
Imports System.Net

Public Class Form1

    Public Event DownloadComplete As EventHandler(Of DownloadCompleteEventArgs)

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim downloader As New WebClient

        AddHandler downloader.DownloadFileCompleted, AddressOf downloader_DownloadFileCompleted

        Dim sourceUri As New Uri("source address here")
        Dim destinationPath = "destination path here"

        downloader.DownloadFileAsync(sourceUri, destinationPath, destinationPath)
    End Sub

    Private Sub downloader_DownloadFileCompleted(sender As Object, e As AsyncCompletedEventArgs)
        Dim downloader = DirectCast(sender, WebClient)

        RemoveHandler downloader.DownloadFileCompleted, AddressOf downloader_DownloadFileCompleted

        Dim filePath = CStr(e.UserState)

        OnDownloadComplete(New DownloadCompleteEventArgs(filePath, e.Cancelled))
    End Sub

    Protected Overridable Sub OnDownloadComplete(e As DownloadCompleteEventArgs)
        RaiseEvent DownloadComplete(Me, e)
    End Sub

End Class

Public Class DownloadCompleteEventArgs
    Inherits EventArgs

    Public ReadOnly Property FilePath As String

    Public ReadOnly Property IsCancelled As Boolean

    Public Sub New(filePath As String, isCancelled As Boolean)
        Me.FilePath = filePath
        Me.IsCancelled = isCancelled
    End Sub

End Class

您首先调用DownloadFileAsync 的重载,它允许您传递数据,然后返回DownloadFileCompleted 事件处理程序。然后,在该事件处理程序中,您可以使用我在博文中概述的步骤,使用您想要的信息引发您自己的自定义事件。

【讨论】:

    猜你喜欢
    • 2018-07-15
    • 2018-05-28
    • 1970-01-01
    • 1970-01-01
    • 2020-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多