[英]Handling errors while using unmanaged code in a managed one ( C++, C, C++/CLI, C#)
[英]event handling using c++/cli and using it in c#
我想在调用showmessage方法时引发一个事件,我想用C#代码捕获它。
我已经为此写了活动。
-是否正确初始化了将委托与函数showmessage关联的初始化函数
-如何在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是上面所有声明和定义的类的名称
C#
void display(string progressnotification)
{
Console.Out.WriteLine(progressnotification);
}
void initialize()
{
first = new Mclass();
first.notify()+=display;
}
这成功了
为什么不能只在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)
{
}
}
我还没有尝试过。 但我认为值得一试。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.