[英]What's the correct way to convert this event handler registration from C# to VB.net?
我正在尝试将一些代码从C#移植到VB.net,但是我在使用简单的事件处理程序时遇到了麻烦。
C#:
hook.KeyPressed += new EventHandler<KeyPressedEventArgs>(hook_KeyPressed);
我已经在VB.net中这样写了:
AddHandler hook.KeyPressed, AddressOf hook_KeyPressed
但是我的转换缺少C#代码中对KeyPressedEventArgs的任何引用,而且我不确定我是否做对了。 任何帮助,将不胜感激。
请记住,事件是委托。 若要订阅事件,事件处理程序的签名必须与委托相同。 只要您的方法具有正确的签名,就可以正确完成。 只要确保事件处理程序的EventArgs参数为KeyPressedEventArgs类型即可。
C#:
hook.KeyPressed += new EventHandler<KeyPressedEventArgs>(hook_KeyPressed);
新的C#语法:
hook.KeyPressed += hook_KeyPressed;
VB.net
AddHandler hook.KeyPressed, AddressOf hook_KeyPressed
VB.net中的处理程序:
Sub hook_KeyPressed(ByVal sender As Object, ByVal e As KeyPressedEventArgs)
'code here
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.