如何在 VB.NET 中添加事件处理程序?

发布于 2024-12-26 16:42:34 字数 294 浏览 0 评论 0原文

此代码是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

爱她像谁 2025-01-02 16:42:34

开始吧:

AddHandler AsyncFileUpload1.UploadedComplete, AddressOf AsyncFileUpload1_UploadedComplete

或者,在代码中,您可以从左侧下拉列表(就在代码上方)中选择 AsyncFileUpload1 控件,然后从以下位置选择 UploadComplete 事件:右侧的下拉列表。

这将使用 VB Handles 声明自动创建具有正确签名的事件处理程序。

Here you go:

AddHandler AsyncFileUpload1.UploadedComplete, AddressOf AsyncFileUpload1_UploadedComplete

Alternatively, within your code, you can select the AsyncFileUpload1 control from the left-hand dropdown list (just above the code) and then select the UploadComplete event from the right-hand dropdown list.

This will automatically create an event handler with the correct signature using the VB Handles declaration.

乖乖 2025-01-02 16:42:34

其他人已经展示了如何在 VB 中将 event+= 逐字翻译为 AddHandler

然而,尽管有相似之处,VB 和 C# 是不同的语言,从字面上翻译,好的 C# 代码可能不是好的 VB 代码。例如,在 VB 中,将固定事件处理程序附加到 ASP.NET 控件的规范方法是使用 Handles 关键字:

Protected Sub AsyncFileUpload1_UploadedComplete(sender As Object, _
                                                e As AsyncFileUploadEventArgs) _
    Handles AsyncFileUpload1.UploadedComplete

    ' Your event handler code is here

End Sub

Others have shown how to literally translate event+= to AddHandler 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:

Protected Sub AsyncFileUpload1_UploadedComplete(sender As Object, _
                                                e As AsyncFileUploadEventArgs) _
    Handles AsyncFileUpload1.UploadedComplete

    ' Your event handler code is here

End Sub
空城缀染半城烟沙 2025-01-02 16:42:34

如果您可以将该代码放入可编译的 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

美人如玉 2025-01-02 16:42:34

有两种方法可以实现此目的:

如果您的 AsyncFileUpload1 变量具有 WithEvents 限定符,您可以在事件处理程序本身上使用 Handles 关键字执行以下操作:

Private Sub AsyncFileUpload1_UploadedComplete(ByVal sender As Object, ByVal e As AsyncFileUploadEventArgs) Handles AsyncFileUpdate1.UploadedComplete

    'handler logic...

End Sub

如果没有 WithEvents 限定符,然后执行以下操作:

AddHandler AsyncFileUpload1.UploadedComplete, AddressOf AsyncFileUpload1_UploadedComplete

要删除事件处理程序,请执行以下操作:

RemoveHandler AsyncFileUpload1.UploadedComplete, AddressOf AsyncFileUpload1_UploadedComplete

注意 WithEvents/Handles 路由,因为这可能会导致 内存泄漏。它只是语法糖,并在幕后连接 AddHandler。我添加此内容是因为我之前在学习 VB 时曾被它烧伤过(我有 C# 背景)。

Two ways to do this:

If your AsyncFileUpload1 variable has the WithEvents qualifier, you can do the following using the Handles keyword on the event handler itself:

Private Sub AsyncFileUpload1_UploadedComplete(ByVal sender As Object, ByVal e As AsyncFileUploadEventArgs) Handles AsyncFileUpdate1.UploadedComplete

    'handler logic...

End Sub

If there is no WithEvents qualifier, then the following works:

AddHandler AsyncFileUpload1.UploadedComplete, AddressOf AsyncFileUpload1_UploadedComplete

To remove the event handler, do the following:

RemoveHandler AsyncFileUpload1.UploadedComplete, AddressOf AsyncFileUpload1_UploadedComplete

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文