如何在C ++/CLI中调用事件处理程序? [英] How to invoke an event handler in C++/CLI ?

查看:61
本文介绍了如何在C ++/CLI中调用事件处理程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#中,可以定义一个事件委托&因此,处理程序:

In C#, one can define an event delegate & handler thusly:

class SomeClass
{
    public delegate void SomeEventHandler(object sender, int n);
    public event SomeEventHandler SomeEvent;

    public void RaiseEvent(int n)
    {
        if (SomeEvent != null)
            SomeEvent(this, n);
    }

}



那么一些耗时的代码可以做到这一点:



then some consuming code could do this:

SomeClass sc = new SomeClass();
sc.SomeEvent += new SomeClass.SomeEventHandler(sc_OnSomeEvent);
sc.RaiseEvent(2);


这将导致使用2调用sc_OnSomeEvent().

我在用C ++/CLI编写代码时遇到了麻烦.
这是我到目前为止的内容:


which would cause sc_OnSomeEvent() to be called with 2.

I''m having some trouble writing this in C++/CLI.
Here''s what I have so far:

public ref class CppCliClass
{
public:
    delegate void CppCliEventHandler(int n);
    event CppCliEventHandler^ OnCppCliEvent;

    void RaiseCppCliEvent(int n)
}



但我在编写RaiseCppCliEvent()时遇到问题.
基于C#语法,我希望它像这样编码:



but I''m having trouble writing RaiseCppCliEvent().
Based on the C# syntax, I''d expect it to be coded like this:

void CppCliClass::RaiseCppCliEvent(int n)
{
    if(OnCppCliEvent != nullptr)
        OnCppCliEvent(this, n)
}



但是我得到了这些编译器错误:



but I get these compiler errors:

error C3918: usage requires ''CppCliClass::OnCppCliEvent'' to be a data member
error C2660: ''CppCliClass::OnCppCliEvent::raise'' : function does not take 2 arguments



从RaiseCppCliEvent()内部获取有关OnCppCliEvent()的正确C ++/CLI调用语法的任何建议?



Any suggestions on the correct C++/CLI calling syntax for OnCppCliEvent() from within RaiseCppCliEvent() ?

推荐答案

是的,这是一个众所周知的问题— C ++/CLI是非常不一样.解决方案也是众所周知的,但看起来可能很不寻常:不仅不允许您进行检查,如if(OnCppCliEvent != nullptr)一样,您也永远不需要它

换句话说,只需删除此检查-代码即可正常工作.如果未添加事件处理程序,则不会自动调用代码调用事件.难以置信,但请尝试!

这是最简单的用法模式:

Yes, this is well-known problem — C++/CLI is very different. Solution is also well-known, but it might look very unusual: not only you are not allowed to do the check like if(OnCppCliEvent != nullptr), you also never need it!

In other words, just delete this check — the code will work correctly. If event handler is not added, the code invoking event will not be called, automatically. Hard to believe, but try it!

Here is the simplest usage pattern:

ref class EventSample {
public:
    event System::EventHandler^ SomeEvent;
private:
    void FireEvent() {
        //no check-up for nullptr SomeEvent is required here
        //event instance is not a method, if it won't be called in the line above it it's null:
        SomeEvent(this, gcnew System::EventArgs());
    }
    //...
};



事件实例不是方法,它是某个类(每个所使用的每个事件参数类都是隐藏的,自动发射的)的实例,该类中有一个调用列表,事件本身不被调用,处理程序存储在调用列表中.我在我的文章中对此进行了解释:动态方法分派器 [



Event instance is not a method, it is a instance of some (hidden, auto-emitted per each event argument class used) class which has an invocation list in it, event per se is not called, handlers stored in the invocation lists are. I explained it in my article: Dynamic Method Dispatcher[^]. Sorry, the article in C# where the check for null is actually allowed and needed, but it is not so in C++/CLI.

—SA


CppCliEventHandler仅将整数作为参数更新它以反映您的新设计.
例如
CppCliEventHandler only takes an integer as parameter update it to reflect your new design.
E.g.
delegate void CppCliEventHandler(CppCliClass^ sender, int n);


这篇关于如何在C ++/CLI中调用事件处理程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆