简体   繁体   English

使用c ++ / cli并在c#中使用它进行事件处理

[英]event handling using c++/cli and using it in c#

-I want to raise an event whenever method showmessage is called.I want to catch it in C# code. 我想在调用showmessage方法时引发一个事件,我想用C#代码捕获它。

-I have written the event for it. 我已经为此写了活动。

-Is it correct what i have done in Initialize function to associate the delegate with the function showmessage -是否正确初始化了将委托与函数showmessage关联的初始化函数

-how to use this event in c# -如何在C#中使用此事件

C++/CLI

delegate void progressmsgdisplay(System::String ^ message);
progressmsgdisplay  ^ progressMsgNotify;
void Mclass::ShowMessage(System::String ^ message)
{ 
MessageBox(NULL, m_msg, m_box, NULL);
notify(message);
}
event progressmsgdisplay ^ notify 
{
    void add(progressmsgdisplay ^ d) 
    {
        progressMsgNotify += d;
    }


    void remove(progressmsgdisplay ^ d) 
    {
        progressMsgNotify -= d;
    }


    void raise(System::String ^ msg)
    {
       progressmsgdisplay ^ tmp = progressMsgNotify;
               if (tmp) 
        {
        tmp->Invoke(msg);
        }

    }
}

//void Mclass::Initialize(System::String ^ strProgressMsg)
//{
// progressMsgNotify=gcnew progressmsgdisplay(this,&Mclass::ShowMessage);
//}

-Mclass is the name of the class in which all of the above is declared and defined -Mclass是上面所有声明和定义的类的名称

C#
void display(string progressnotification)
 {
    Console.Out.WriteLine(progressnotification);
 }
void initialize()
{
 first = new Mclass();
 first.notify()+=display;
}

this did the trick 这成功了

Why can't you just use EventHandler class in c++/cli and subscribe it in C# 为什么不能只在c ++ / cli中使用EventHandler类并在C#中订阅它

//C++/CLI
public ref class SomeClass
{
    public:
    event EventHandler^ someEvent;
}

//C#
class Program
{
    static void Main(string[] args)
    {
        SomeClass testclass = new SomeClass();
        testclass.someEvent += someEventHandler;
    }

    private void someEventHandler(Object obj, EventArgs args)
    {

    }
}

I haven't tried this yet. 我还没有尝试过。 But I guess its worth a try. 但我认为值得一试。

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

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