[英]Converting delegate from C# to VB.NET
我试图将我的C#代码转换为VB.NET,但是我被卡住了。
我收到的错误是“私人事件FixationReceived(Byref数据为Fixation,userdata为System.IntPtr)是一个事件,不能直接调用。使用RaiseEvent引发事件。
我原来的C#代码是:
private event FixationDelegate FixationReceived;
这就是我设置委托处理程序的方式:
FixationReceived += new FixationDelegate(SharpClient_FixationReceived);
IntPtr p;
p = Marshal.GetFunctionPointerForDelegate(FixationReceived);
_api.SetFixationCB(p.ToInt32(), IntPtr.Zero);
private void SharpClient_FixationReceived(ref Fixation data, IntPtr userData)
{
if (InvokeRequired)
{
BeginInvoke(new FixationDelegate(SharpClient_FixationReceived), new object[] { data, userData });
}
else
{
eventStreamLabel.Text = "Fix Start: " + data.timeStamp.ToString() + " Duration: " + data.duration.ToString();
}
}
我尝试使用以下方法将其转换为VB.NET:
Private Event FixationReceived As FixationDelegate
Private Sub VBNET_FixationReceived(ByRef data As Fixation, userData As System.IntPtr)
Stop'is never called :-(
End Sub
这就是我设置处理程序的方式:
Dim p As IntPtr
'In the next line the stated error is raised
p = Marshal.GetFunctionPointerForDelegate(FixationReceived)
_api.SetFixationCB(p.ToInt32(), IntPtr.Zero) 'this works fine
AddHandler(FixationEvent, AddressOf VBNET_FixationReceived )'here I am getting the error "FixationEvent is not an event of MyApplication.Form1"
BeginInvoke(New fixationDelegate(AddressOf vbnet_fixationreceived )'Here I get an error without any further explanation.
我没有将C#委托转换为VB.NET的经验,也许有人可以对我的错误有所了解。
非常感谢你!
尝试这个
AddHandler FixationDelegate, AddressOf SharpClient_FixationReceived
Dim p As IntPtr
p = Marshal.GetFunctionPointerForDelegate(FixationReceived)
_api.SetFixationCB(p.ToInt32(), IntPtr.Zero)
-
Private Sub SharpClient_FixationReceived(ByRef data As Fixation, userData As IntPtr)
If InvokeRequired Then
BeginInvoke(New FixationDelegate(AddressOf SharpClient_FixationReceived), New Object() {data, userData})
Else
eventStreamLabel.Text = "Fix Start: " + data.timeStamp.ToString() + " Duration: " + data.duration.ToString()
End If
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.