我正在尝试将我的C#代码转换为VB.NET,但我被卡住了。
我得到的错误是“Private Event FixationReceived(Byref data As Fixation,userdata As 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的经验,也许有人可以解释我的错误。
非常感谢!
答案 0 :(得分:0)
试试这个
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