如何在 VB.NET 中使用以下用 C# 编写的事件/委托?
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
+=
运算符基本上是将FeedbackService.OnError
函数订阅到Error
调用列表。因此,当引发Error
事件时,将调用OnError
方法。要将上述代码转换为 VB.NET,它看起来类似于:
请参阅如何:引发和使用事件,用于 VB.NET 中的一些示例。
The
+=
operator is basically subscribing theFeedbackService.OnError
function to theError
invocation list. So when theError
event is raised, theOnError
method is invoked.To translate the above code to VB.NET, it would look something like:
See How to: Raise and Consume Events for some examples in VB.NET.
恐怕我不确定 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)