在C ++/CLI中处理关键字? [英] Handles keyword in C++/CLI ?

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

问题描述

大家好.抱歉,如果我的问题太基础了,但是我还没有找到答案.
我有一个在DLL中编译的VB.NET类:

Hello guys. Sorry if my question is too basic, but I haven''t found the answer.
I have a VB.NET class which is compiled in a DLL:

Public class PiArc

    Dim m_EnableEvents as Boolean

    Private Sub m_Modified()
        If m_EnableEvents = True Then
            RaiseEvent OnModify()
        End If
    End Sub

    Public Event OnModify()
end class


我在另一个dll中有一个C ++/CLI类,该类是从第一个派生的:


And I have a C++/CLI class in a different dll, which is derived from the first:

public ref class GfxArc : public Geometry::PiArc{
public:
    GfxArc(){ //constructor
        //ALL I need is to attach a handler the event raised in the (base)object.
    };
    void onRefreshProperty(){
         //REFRESH stuff
    };
};


我需要将处理程序附加到基类中定义的事件.
在VB.NET中,它很简单,您只需使用Handles关键字附加事件处理程序即可.在C ++/CLI中也可能吗?怎么样?
谢谢


I need to attach a handler to the event defined in the base class.
In VB.NET it''s simple, you just use the Handles keyword to attach an event handler. Is this also possible in C++/CLI? How?
Thank you

推荐答案

它相当简单,请参见此MSDN文章:

http://msdn.microsoft.com/en-us/library/ms235237 (v = vs.100).aspx [
It''s fairly straightforward, see this MSDN article:

http://msdn.microsoft.com/en-us/library/ms235237(v=vs.100).aspx[^]

That said, since this is a parent-class - child-class situation, you may also want to look at making it a virtual method in the base class and then overriding it in the derived.


public ref class GfxArc : public Geometry::PiArc{
public:
    void onRefreshProperty(){
         //REFRESH stuff
    };

    GfxArc(){ //constructor
        this->OnModify += gcnew Geometry::PiArc::OnModifyEventHandler( this, 
                          &GfxArc::onRefreshProperty );
    }; 
};


在VB.NET中创建事件时,将在后台创建事件处理程序(在这种情况下为OnModifyEventHandler).
我花了半天才找到答案.


When you make an event in VB.NET, an event handler in the background is made (OnModifyEventHandler in this case).
I spent half a day until I found this out.


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

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