如何在VB.NET中使用以下用C#编写的事件/代理? [英] How can I use the following events/delegates, written in C#, in VB.NET?
问题描述
我在ASP.NET网络应用中使用JdSoft的APNS-Sharp库。该库是用C#编写的,并且出于线程目的广泛使用了委托函数和事件。我的应用程序是用VB.NET编写的,我对如何转换以下示例代码(C#)感到有些困惑:
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(...);
}
以下是FeedbackService类的相关成员:
Here are the relevant members of the FeedbackService class:
public delegate void OnError(object sender, Exception ex);
public event OnError Error;
基本上,我试图弄清楚如何将函数(如service_Error)附加到事件上(如错误)在VB.NET中。我不清楚在这种情况下+ =语法的含义,VisualStudio表示由于某些原因我的VB.NET代码无法直接访问错误事件。
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.
推荐答案
+ =
运算符基本上是在订阅 FeedbackService.OnError
函数添加到 Error
调用列表。因此,当引发 Error
事件时,将调用 OnError
方法。
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.
要将上面的代码转换为VB.NET,看起来应该像这样:
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
请参见如何进行:引发和消耗事件,用于VB.NET中的某些示例。
See How to: Raise and Consume Events for some examples in VB.NET.
这篇关于如何在VB.NET中使用以下用C#编写的事件/代理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!