如何在 VB.NET 中添加事件处理程序?
此代码是 AjaxControlToolkitSampleSite
的一部分。确切地说,它位于 AsyncFileUpload
控件中:
AsyncFileUpload1.UploadedComplete += new EventHandler<AsyncFileUploadEventArgs>(AsyncFileUpload1_UploadedComplete);
How can Itranslate this to VB.NET?
This code is part of AjaxControlToolkitSampleSite
. To be exact, it is in the AsyncFileUpload
control:
AsyncFileUpload1.UploadedComplete += new EventHandler<AsyncFileUploadEventArgs>(AsyncFileUpload1_UploadedComplete);
How can I translate this to VB.NET?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
开始吧:
或者,在代码中,您可以从左侧下拉列表(就在代码上方)中选择
AsyncFileUpload1
控件,然后从以下位置选择UploadComplete
事件:右侧的下拉列表。这将使用 VB
Handles
声明自动创建具有正确签名的事件处理程序。Here you go:
Alternatively, within your code, you can select the
AsyncFileUpload1
control from the left-hand dropdown list (just above the code) and then select theUploadComplete
event from the right-hand dropdown list.This will automatically create an event handler with the correct signature using the VB
Handles
declaration.其他人已经展示了如何在 VB 中将
event+=
逐字翻译为AddHandler
。然而,尽管有相似之处,VB 和 C# 是不同的语言,从字面上翻译,好的 C# 代码可能不是好的 VB 代码。例如,在 VB 中,将固定事件处理程序附加到 ASP.NET 控件的规范方法是使用
Handles
关键字:Others have shown how to literally translate
event+=
toAddHandler
in VB.However, despite the similarities, VB and C# are different languages, and good C# code might not be good VB code when translated literally. For example, in VB, the canonical way to attach a fixed event handler to an ASP.NET control is by using the
Handles
keyword:如果您可以将该代码放入可编译的 C# 项目中,则可以使用 SharpDevelop 将该项目转换为 VB.NET 。这可能是在 C# 和 VB.NET 之间转换的最佳方式。
此外,ILSpy 可以将用 C# 编写的已编译 dll 转换为 VB.NET
If you can put that code in a C# project that compiles, you can convert that project to VB.NET with SharpDevelop. This is probably the best way to translate between C# and VB.NET.
Also, ILSpy can translate a compiled dll written in C# into VB.NET
有两种方法可以实现此目的:
如果您的
AsyncFileUpload1
变量具有WithEvents
限定符,您可以在事件处理程序本身上使用 Handles 关键字执行以下操作:如果没有
WithEvents
限定符,然后执行以下操作:要删除事件处理程序,请执行以下操作:
注意
WithEvents/Handles
路由,因为这可能会导致 内存泄漏。它只是语法糖,并在幕后连接 AddHandler。我添加此内容是因为我之前在学习 VB 时曾被它烧伤过(我有 C# 背景)。Two ways to do this:
If your
AsyncFileUpload1
variable has theWithEvents
qualifier, you can do the following using the Handles keyword on the event handler itself:If there is no
WithEvents
qualifier, then the following works:To remove the event handler, do the following:
Beware of the
WithEvents/Handles
route as this can cause memory leaks. It is simply syntactic sugar and wires up an AddHandler behind the scenes. I add this because I've been burned before with it while learning VB (I had a C# background).