简体   繁体   English

VB.NET问题与“控制数组”

[英]VB.NET Problem with “Control Arrays”

I have a VB.NET application and use some third party (closed source) ActiveX controls. 我有一个VB.NET应用程序,并使用了一些第三方(封闭源)ActiveX控件。 One of the controls represents a "camera" (connected over several interfaces) and I try to write an example how to work with several cameras in one application. 其中一个控件代表一个“摄像机”(通过多个接口连接),我尝试编写一个示例,说明如何在一个应用程序中使用多个摄像机。 To do this I allocate multiple "camera" objects dynamically as an array which works as expected like this: 为此,我将多个“相机”对象动态分配为一个数组,该数组按预期方式工作,如下所示:

Const NUM_CAMERAS = 2
Private MyCameras(NUM_CAMERAS ) As xxx.MCamera

But the camera objects needs to be allocated with WithEvents because they raise events when a new image was taken. 但是需要为相机对象分配WithEvents因为它们会在拍摄新图像时引发事件。 I found out that WithEvents variables cannot be typed as arrays and this is a pretty common problem so I also found some workarounds: http://www.dreamincode.net/code/snippet885.htm http://www.xtremevbtalk.com/archive/index.php/t-223970.html 我发现WithEvents变量无法键入为数组,这是一个非常普遍的问题,因此我也找到了一些解决方法: http : //www.dreamincode.net/code/snippet885.htm http://www.xtremevbtalk.com/存档/ index.php的/叔223970.html

This is already pretty helpful and I adopted this to my concept. 这已经很有帮助,我将其采纳为我的概念。 So I have a MyCameras array and a MyCamera all "without Events", first allocate a new MyCamera object, add a event handler and then put it into the array. 因此,我有一个MyCameras数组和一个MyCamera所有“没有事件”,首先分配一个新的MyCamera对象,添加一个事件处理程序,然后将其放入数组中。 Unfortunately I get an error when calling 不幸的是我打电话时遇到错误

AddHandler Camera.ProcessModifiedImage, AddressOf MyHook

Normally "MyHook" is declared as 通常,“ MyHook”被声明为

Private Sub MyHook (ByVal sender As Object, ByVal ModifiedBuffer As xxx.ProcessModifiedImageEvent) Handles Camera.ProcessModifiedImage

Like in the "Button examples" I just removed the "Handles Camera.ProcessModifiedImage" but I get an error that "MyHook" has not the same signature as the Delegate 就像在“按钮示例”中一样,我只是删除了“ Handles Camera.ProcessModifiedImage”,但是我收到一个错误,即“ MyHook”的签名与委托的签名不同

Delegate Sub ICameraEvents_ProcessModifiedImageEventHandler(ImageIndex as Integer)

Has anyone an idea how to get around this and what to change? 有谁知道如何解决这个问题以及要进行哪些更改? I can post more code and details tomorrow if necessary. 如有必要,我明天可以发布更多代码和详细信息。

The "MyHook" event handler needs to have the same signature (ie accept the same argument types in the same order) as the delegate. “ MyHook”事件处理程序需要与委托具有相同的签名(即,以相同的顺序接受相同的参数类型)。 Since the desired delegate accepts an integer argument called ImageIndex you could declare the the MyHook event handler as: 由于所需的委托接受一个称为ImageIndex的整数参数,因此可以将MyHook事件处理程序声明为:


Private Sub MyHook(ImageIndex as Integer)

End Sub

Following up on the comments to Waves' post, you can elegantly handle the missing "sender" argument with a lambda expression: 跟随Waves帖子的评论,您可以使用lambda表达式优雅地处理丢失的“ sender”参数:

  Public Sub New()
    InitializeComponent()
    For camera As Integer = 1 To 4
      Dim cam As Camera = DirectCast(Me.Controls("Camera" + camera.ToString), Camera)
      AddHandler cam.ProcessModifiedImage, Function(index As Integer) ProcessImage(cam, index)
    Next
  End Sub

  Private Function ProcessImage(ByVal cam As Camera, ByVal ImageIndex As Integer) As Integer
    ' Your code here
    '...
    Return 0
  End Function

The error is telling you what you need -- your event handler's signature must match the delegate they've specified (a subroutine with one ImageIndex parameter) 错误告诉您您需要什么-事件处理程序的签名必须与他们指定的委托匹配(带有一个ImageIndex参数的子例程)

So you want 所以你要

Private Sub MyHook(ImageIndex as Integer)

You've probably seen all event handlers be in the format sub myhook(sender as object, e as XXEventArgs) 您可能已经看到所有事件处理程序都采用myhook格式(发送方为对象,e为XXEventArgs)

This is a convention microsoft uses for their .NET events, but not required. 这是Microsoft用于其.NET事件的约定,但不是必需的。 Your third party tool apparently doesn't follow that convention. 您的第三方工具显然不遵循该约定。 Just follow the message when it tells you what the delegate signature must look like. 当消息告诉您委托人签名必须是什么样时,只需遵循该消息即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM