C++CLI 中的事件处理

发布于 2024-09-14 02:26:53 字数 1572 浏览 3 评论 0原文

我有一个函数 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

昨迟人 2024-09-21 02:26:53

.NET 中的事件处理程序通常使用两个参数进行定义(请参阅 MSDN)

delegate void EventHandler(object sender, EventArgs e)

但是,您发布的错误消息确实强烈暗示 OnNewFrame 不遵循此模式,并且 singleFrameEventHandler 不应采用任何参数。

我的猜测是,该 API 的开发人员认为事件本身就有足够的信息,并且事件处理程序将存储对引发事件的 IAFrame 的引用(如果需要查询该事件)。类似于以下的代码应该可以工作(请注意,在创建事件处理程序委托时,您需要引用拥有事件处理程序方法的类,而不是源对象):

class FrameEventHandlers
{
public:
    FrameEventHandlers(IAFrame ^ frame)
      : m_frame(frame)
    {
        m_frame->OnNewFrame += gcnew newFrame(this, &FrameEventHandlers::singleFrameEventHandler);
    }

private:
    void singleFrameEventHandler()
    {
        // query m_frame for new data
    }
};

Event handlers in .NET are typically defined with two parameters (see MSDN)

delegate void EventHandler(object sender, EventArgs e)

However, the error messages you've posted do strongly imply that OnNewFrame does not follow this pattern and that singleFrameEventHandler 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):

class FrameEventHandlers
{
public:
    FrameEventHandlers(IAFrame ^ frame)
      : m_frame(frame)
    {
        m_frame->OnNewFrame += gcnew newFrame(this, &FrameEventHandlers::singleFrameEventHandler);
    }

private:
    void singleFrameEventHandler()
    {
        // query m_frame for new data
    }
};
半暖夏伤 2024-09-21 02:26:53

第一种语法不起作用,这是 C++/CLI 没有的 C# 语法糖。

第二个片段的问题是委托类型与事件处理程序的签名不匹配。委托是这样声明的:

 delegate void newFrame();

您必须将方法签名更改为:

 void singleFrameEventHandler() 
 {
    // etc..
 }

从您的代码片段中不清楚您应该如何获取对框架的引用。我猜想是班级里的一个财产。

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:

 delegate void newFrame();

You'll have to change your method signature to:

 void singleFrameEventHandler() 
 {
    // etc..
 }

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文