如何在 VB.NET 中使用以下用 C# 编写的事件/委托?

发布于 2024-12-07 19:22:36 字数 621 浏览 0 评论 0原文

我在 ASP.NET Web 应用程序中使用 JdSoft 的 APNS-Sharp 库。该库是用 C# 编写的,并广泛使用委托函数和事件来实现线程化目的。我的应用程序是用 VB.NET 编写的,我对如何翻译以下示例代码 (C#) 感到有点困惑:

....
//Wireup the events
service.Error += new FeedbackService.OnError(service_Error);
....
}

static void service_Error(object sender, Exception ex)
{
Console.WriteLine(...);
}

以下是 FeedbackService 类的相关成员:

public delegate void OnError(object sender, Exception ex);
public event OnError Error;

基本上,我试图弄清楚如何在 VB.NET 中将函数(如 service_Error)附加到事件(如 Error)。我不清楚 += 语法在此上下文中的含义,并且 VisualStudio 表示由于某种原因,我的 VB.NET 代码无法直接访问“Error”事件。

I'm using JdSoft's APNS-Sharp library in my ASP.NET web app. The library is written in C#, and makes extensive use of Delegate Functions and Events for threading purposes. My application is written in VB.NET, and I'm a little confused on about how to translate the following sample code (C#):

....
//Wireup the events
service.Error += new FeedbackService.OnError(service_Error);
....
}

static void service_Error(object sender, Exception ex)
{
Console.WriteLine(...);
}

Here are the relevant members of the FeedbackService class:

public delegate void OnError(object sender, Exception ex);
public event OnError Error;

Basically, I'm trying to figure out how to attach a function (like service_Error) to an event (like Error) in VB.NET. I'm unclear on what the += syntax means in this context, and VisualStudio says that the 'Error' event cannot be accessed directly by my VB.NET code for some reason.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

桃气十足 2024-12-14 19:22:36

+= 运算符基本上是将 FeedbackService.OnError 函数订阅到 Error 调用列表。因此,当引发 Error 事件时,将调用 OnError 方法。

要将上述代码转换为 VB.NET,它看起来类似于:

// define delelgate/event
Public Delegate Sub OnError(sender As Object, ex As Exception)
Public Event OnError Error

// attach method to event
AddHandler service.Error, service_Error

请参阅如何:引发和使用事件,用于 VB.NET 中的一些示例。

The += operator is basically subscribing the FeedbackService.OnError function to the Error invocation list. So when the Error event is raised, the OnError method is invoked.

To translate the above code to VB.NET, it would look something like:

// define delelgate/event
Public Delegate Sub OnError(sender As Object, ex As Exception)
Public Event OnError Error

// attach method to event
AddHandler service.Error, service_Error

See How to: Raise and Consume Events for some examples in VB.NET.

回忆那么伤 2024-12-14 19:22:36
AddHandler service.Error, service_Error
AddHandler service.Error, service_Error
一瞬间的火花 2024-12-14 19:22:36

恐怕我不确定 VB 实现,但是 C# 中关于委托的 += 语法将一个方法添加到委托方法列表(调用列表)

I'm not sure on the VB implementation I'm afraid, but the += syntax in C# with respect to delegates, adds a method to the delegates list of methods (invocation list)

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