[英]Using Delegates in vb.net
I'm new to vb.net and trying to build a win CE app in vb.net. 我是vb.net的新手,正在尝试在vb.net中构建Win CE应用程序。 The sdk of the handheld device is in C# which I converted with an online converter to vb.net. 手持设备的sdk是C#,我使用在线转换器将其转换为vb.net。 Below is the C# code: 下面是C#代码:
public class DecodeEventArgs : EventArgs
{
private string barcode;
private byte type;
public DecodeEventArgs(string barcodeData, byte typeData)
{
barcode = barcodeData;
type = typeData;
}
public string Barcode
{
get { return barcode; }
set { barcode = value; }
}
public byte Type
{
get { return type; }
set { type = value; }
}
}
Converted to vb.net as: 转换为vb.net为:
Public Class DecodeEventArgs
Inherits EventArgs
Public barcode As String
Public type As Byte
Public Sub New(ByVal barcodeData As String, ByVal typeData As Byte)
barcode = barcodeData
type = typeData
End Sub
Public Property pBarcode() As String
Get
Return barcode
End Get
Set(ByVal value As String)
barcode = value
End Set
End Property
Public Property pType() As Byte
Get
Return type
End Get
Set(ByVal value As Byte)
type = value
End Set
End Property
End Class
In my conversion from C# I added a 'p' to the name of the Properties as you see in my vb.net code because I had a error that said 在从C#转换时,我在vb.net代码中看到的属性名称中添加了'p',因为我遇到了一个错误
Barcode is already declared as a public string in this class 条形码已在此类中声明为公共字符串
I'm not sure if that is part of my problem but my real issue is, on a form they used .BeginInvoke to call the class with this code: 我不确定这是否是我的问题的一部分,但我真正的问题是,在他们使用.BeginInvoke的表单上,使用以下代码调用该类:
void scanner_DecodeEvent(object sender, DecodeEventArgs e)
{
Win32.sndPlaySound(Properties.Resources.Scan, Win32.SND_ASYNC | Win32.SND_MEMORY);
this.BeginInvoke((Action<string>)delegate(string barcode)
{
scanCount = 0;
ListViewItem item = new ListViewItem(new string[] { barcode });
lstView.Items.Insert(0, item);
}, e.Barcode);
}
Which I converted to vb.net as: 我将其转换为vb.net的方式是:
Private Sub scanner_DecodeEvent(ByVal sender As Object, ByVal e As DecodeEventArgs)
PlaySound()
Me.BeginInvoke(DirectCast(Barcode As String) )
scanCount = 0
Dim item As New ListViewItem(New String() {barcode})
lstView.Items.Insert(0, item)
End Sub
Which gives me an error about Barcode not being declared. 这给我一个关于未声明条形码的错误。 This is where I'm stuck. 这就是我卡住的地方。 Thanks for your assistance in advance 多谢您的协助
That C# snippet creates an anonymous method which it invokes to perform the actions on the UI thread, and it sends e.Barcode
as a parameter. 该C#代码段创建了一个匿名方法,该方法将调用该匿名方法在UI线程上执行操作,然后将e.Barcode
作为参数发送。 Your VB.NET conversion only tries to invoke some strange, and incomplete, use of DirectCast
. 您的VB.NET转换仅尝试调用DirectCast
某些奇怪DirectCast
完整的用法。 DirectCast
is not needed in the VB.NET conversion as you don't have any delegate
keyword which you must cast to a delegate method. VB.NET转换中不需要DirectCast
,因为您没有任何必须转换为委托方法的delegate
关键字。
Your most simple solution would be to use a Lambda method: 您最简单的解决方案是使用Lambda方法:
Me.BeginInvoke(Sub() 'Lambda anonymous method.
scanCount = 0
Dim item As New ListViewItem(New String() {e.Barcode})
lstView.Items.Insert(0, item)
End Sub)
EDIT: 编辑:
Since you get errors when using the lambda expression I assume you target .NET Framework 3.5 or lower. 由于使用lambda表达式时会出错,因此我假设您的目标是.NET Framework 3.5或更低版本。 For that matter it gets a bit more complicated as you must now put the code in a different method: 为此,它变得更加复杂,因为您现在必须将代码放入其他方法中:
Private Sub AddBarcode(ByVal Barcode As String)
scanCount = 0
Dim item As New ListViewItem(New String() {Barcode})
lstView.Items.Insert(0, item)
End Sub
Then you must declare your own delegate method which you can use to perform the invocation: 然后,您必须声明自己的委托方法,可用于执行调用:
Delegate Sub AddBarcodeDelegate(ByVal Barcode As String)
Private Sub scanner_DecodeEvent(ByVal sender As Object, ByVal e As DecodeEventArgs)
Me.BeginInvoke(New AddBarcodeDelegate(AddressOf AddBarcode), e.Barcode)
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.