语法添加事件处理程序在VB.NET [英] Syntax for adding an event handler in VB.NET

查看:176
本文介绍了语法添加事件处理程序在VB.NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下codeI需要转换到VB.NET。问题是每一个翻译工具,我发现是转换附加处理的部分错误。我似乎不能够通过自己做到这一点。

  FtpClient的FtpClient的=新FtpClient的();
ftpClient.UploadProgressChanged + =新的EventHandler< UploadProgressChangedLibArgs>(ftpClient_UploadProgressChanged);
ftpClient.UploadFileCompleted + =新的EventHandler< UploadFileCompletedEventLibArgs>(ftpClient_UploadFileCompleted);
 

解决方案

有两种不同的方式将事件处理方法,在VB.NET的事件相关联。

第一个涉及使用的手柄关键字,你附加到事件处理程序方法的定义结束。例如:

 子ftpClient_UploadProgressChanged(发送者为对象,E作为UploadProgressChangedLibArgs)处理ftpClient.UploadProgressChanged
    '...
结束小组

子ftpClient_UploadFileCompleted(发送者为对象,E作为UploadFileCompletedEventLibArgs)处理ftpClient.UploadFileCompleted
    '...
结束小组
 

第一种方法是非常简单的,如果你已经有了单独定义事件处理方法呢(比如,如果你不使用lambda语法)。我会推荐它只要有可能。

第二个涉及在C#中明确使用了的AddHandler 语句,就像 + = 。这是你需要使用,如果你想将事件处理程序的动态的,如联想一如果你需要在运行时改变它们。所以,你的code,从字面上转换,应该是这样的:

  FtpClient的昏暗作为新FtpClient的()
AddHandler的ftpClient.UploadProgressChanged,AddressOf ftpClient_UploadProgressChanged
AddHandler的ftpClient.UploadFileCompleted,AddressOf ftpClient_UploadFileCompleted
 

就像你说的,我试图通过开发Fusion的运行您的code转换器,惊讶地看到,他们正在返回无效VB.NET code:

 错误code!
FtpClient的昏暗作为新FtpClient的()
ftpClient.UploadProgressChanged + =新的EventHandler(UploadProgressChangedLibArgs中)(ftpClient_UploadProgressChanged)
ftpClient.UploadFileCompleted + =新的EventHandler(UploadFileCompletedEventLibArgs中)(ftpClient_UploadFileCompleted)
 

事实证明,这是一个已知的bug ,可能是值得的投票!

I have following code i need to convert to VB.NET. Problem is every translation tool I found is converting the add handler part wrong. I don't seem to be able to do it by myself.

FtpClient ftpClient = new FtpClient();
ftpClient.UploadProgressChanged += new EventHandler<UploadProgressChangedLibArgs>(ftpClient_UploadProgressChanged);
ftpClient.UploadFileCompleted += new EventHandler<UploadFileCompletedEventLibArgs>(ftpClient_UploadFileCompleted);

There are two different ways to associate event handler methods with an event in VB.NET.

The first involves the use of the Handles keyword, which you append to the end of the event handler method's definition. For example:

Sub ftpClient_UploadProgressChanged(sender As Object, e As UploadProgressChangedLibArgs) Handles ftpClient.UploadProgressChanged
    ' ...
End Sub

Sub ftpClient_UploadFileCompleted(sender As Object, e As UploadFileCompletedEventLibArgs) Handles ftpClient.UploadFileCompleted
    ' ...
End Sub

The first method is much simpler if you've already got separately defined event handler methods anyway (i.e., if you're not using a lambda syntax). I would recommend it whenever possible.

The second involves the explicit use of the AddHandler statement, just like += in C#. This is the one you need to use if you want to associate event handlers dynamically, e.g. if you need to change them at run time. So your code, literally converted, would look like this:

Dim ftpClient As New FtpClient()
AddHandler ftpClient.UploadProgressChanged, AddressOf ftpClient_UploadProgressChanged
AddHandler ftpClient.UploadFileCompleted, AddressOf ftpClient_UploadFileCompleted

Like you said, I tried running your code through Developer Fusion's converter and was surprised to see that they were returning invalid VB.NET code:

' WRONG CODE!
Dim ftpClient As New FtpClient()
ftpClient.UploadProgressChanged += New EventHandler(Of UploadProgressChangedLibArgs)(ftpClient_UploadProgressChanged)
ftpClient.UploadFileCompleted += New EventHandler(Of UploadFileCompletedEventLibArgs)(ftpClient_UploadFileCompleted)

Turns out, that's a known bug that might be worth voting for!

这篇关于语法添加事件处理程序在VB.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆