C++CLI 中的事件处理
我有一个函数 singleFrameEventHandler,我希望在某个事件 OnNewFrame 发生时调用该函数。经过一些研究后,在我看来,处理事件的函数具有 void 返回类型,并采用包含事件的类型的参数。从我在网上可以找到的内容来看,是这样完成的:
函数声明:
void singleFrameEventHandler(IAFrame ^ frame);
当函数被分配来处理事件时:
iaframe->OnNewFrame += &singleFrameEventHandler;
当我尝试编译包含上述段的代码时,出现以下错误:
error C2664: 'Spiricon::BeamGage::Automation::Interfaces::IAFrame::OnNewFrame::add' : cannot convert parameter 1 from 'void (__clrcall *)(Spiricon::BeamGage::Automation::Interfaces::IAFrame ^)' to 'Spiricon::BeamGage::Automation::Interfaces::newFrame ^'
No user-defined-conversion operator available, or
There is no context in which this conversion is possible
The OnNewFrame is of type Spiricon ::BeamGage::Automation::Interfaces::newFrame,不幸的是,除了描述为“新框架可用时调用的事件”之外,没有任何文档。
在对象浏览器中,我可以找到有关它的一些信息:
public delegate newFrame:
public System.IAsyncResult BeginInvoke(System.AsyncCallback callback, System.Object object)
public System.Void EndInvoke(System.IAsyncResult result)
public System.Void Invoke()
public newFrame(System.Object object, System.IntPtr method)
newFrame 构造函数(?)似乎是最好的选择,因为它似乎采用方法指针,但是当我尝试像这样使用它时:
iaframe->OnNewFrame += gcnew newFrame(iaframe, &singleFrameEventHandler);
我收到以下错误:
error C3352: 'singleFrameEventHandler' : the specified function does not match the delegate type 'void (void)'
这是否意味着我的函数不必带参数?如果是这样,我如何获得有关该活动的信息?
I have a function, singleFrameEventHandler, that I wish to be called when a certain event, OnNewFrame, occurs. After doing some research, it seemed to me that functions which handle events have a void return type and take a parameter of the type that contains the event. From what I could find online, that is done like this:
Function declaration:
void singleFrameEventHandler(IAFrame ^ frame);
When the function is assigned to handle the event:
iaframe->OnNewFrame += &singleFrameEventHandler;
When I try to compile the code containing the above segments, I get the following error:
error C2664: 'Spiricon::BeamGage::Automation::Interfaces::IAFrame::OnNewFrame::add' : cannot convert parameter 1 from 'void (__clrcall *)(Spiricon::BeamGage::Automation::Interfaces::IAFrame ^)' to 'Spiricon::BeamGage::Automation::Interfaces::newFrame ^'
No user-defined-conversion operator available, or
There is no context in which this conversion is possible
The OnNewFrame is of type Spiricon::BeamGage::Automation::Interfaces::newFrame, which unfortunately has no documentation beyond being described as "The event that is called when a new frame is available."
In the object browser, I can find some information about it:
public delegate newFrame:
public System.IAsyncResult BeginInvoke(System.AsyncCallback callback, System.Object object)
public System.Void EndInvoke(System.IAsyncResult result)
public System.Void Invoke()
public newFrame(System.Object object, System.IntPtr method)
The newFrame constructor (?) seems to be the best bet, as it appears to take a method pointer, but when I try to use it like this:
iaframe->OnNewFrame += gcnew newFrame(iaframe, &singleFrameEventHandler);
I get the following error:
error C3352: 'singleFrameEventHandler' : the specified function does not match the delegate type 'void (void)'
Does this mean that my function has to take no parameters? If so, how would I get information about the event?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
.NET 中的事件处理程序通常使用两个参数进行定义(请参阅 MSDN)
但是,您发布的错误消息确实强烈暗示
OnNewFrame
不遵循此模式,并且singleFrameEventHandler
不应采用任何参数。我的猜测是,该 API 的开发人员认为事件本身就有足够的信息,并且事件处理程序将存储对引发事件的 IAFrame 的引用(如果需要查询该事件)。类似于以下的代码应该可以工作(请注意,在创建事件处理程序委托时,您需要引用拥有事件处理程序方法的类,而不是源对象):
Event handlers in .NET are typically defined with two parameters (see MSDN)
However, the error messages you've posted do strongly imply that
OnNewFrame
does not follow this pattern and thatsingleFrameEventHandler
should take no parameters.My guess is that the developers of this API thought that the event itself was enough information, and that the event handler would store a reference to the IAFrame raising the event if it needed to query it. Code similar to the following should work (note that when creating the event handler delegate, you need to reference the class owning the event handler method, not the source object):
第一种语法不起作用,这是 C++/CLI 没有的 C# 语法糖。
第二个片段的问题是委托类型与事件处理程序的签名不匹配。委托是这样声明的:
您必须将方法签名更改为:
从您的代码片段中不清楚您应该如何获取对框架的引用。我猜想是班级里的一个财产。
The first syntax cannot work, that's C# syntax sugar that C++/CLI doesn't have.
The problem with the 2nd snippet is that the delegate type doesn't match the signature of your event handler. The delegate was declared like this:
You'll have to change your method signature to:
How you're supposed to get a reference to the frame is not clear from your snippets. I'd guess at a property on the class.