简体   繁体   English

如何在 C++/CLI 中创建 C# 事件处理程序?

[英]How to create C# event handlers in C++/CLI?

Here is my code:这是我的代码:

private: System::Void button3_Click(System::Object^  sender, System::EventArgs^  e)
{
    System::Speech::Recognition::SpeechRecognizer ^sr = gcnew System::Speech::Recognition::SpeechRecognizer();

    array<String ^> ^strs = gcnew array<String ^> {"Hello", "World"};

    System::Speech::Recognition::Choices ^colors = gcnew System::Speech::Recognition::Choices();
    colors->Add(strs);

    System::Speech::Recognition::GrammarBuilder ^gb = gcnew System::Speech::Recognition::GrammarBuilder();
    gb->Append(colors);

    System::Speech::Recognition::Grammar ^g = gcnew System::Speech::Recognition::Grammar(gb);
    sr->LoadGrammar(g);

    // System::IntPtr ptr = gcnew System::IntPtr(&sr_SpeechRecognized);
    sr->SpeechRecognized += gcnew System::EventHandler<System::Speech::Recognition::SpeechRecognizedEventArgs>(this,&Form1::sr_SpeechRecognized);
}

void sr_SpeechRecognized(System::Object ^sender, System::Speech::Recognition::SpeechRecognizedEventArgs^ e)
{
 }

This code generates the following error此代码生成以下错误

1>------ Build started: Project: SpeechTest, Configuration: Debug Win32 ------
1>  SpeechTest.cpp
1>c:\users\yohan\documents\visual studio 2010\projects\speechtest\speechtest\Form1.h(144): error C3225: generic type argument for 'TEventArgs' cannot be 'System::Speech::Recognition::SpeechRecognizedEventArgs', it must be a value type or a handle to a reference type
1>c:\users\yohan\documents\visual studio 2010\projects\speechtest\speechtest\Form1.h(144): error C3352: 'void SpeechTest::Form1::sr_SpeechRecognized(System::Object ^,System::Speech::Recognition::SpeechRecognizedEventArgs ^)' : the specified function does not match the delegate type 'void (System::Object ^,System::Speech::Recognition::SpeechRecognizedEventArgs)'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

In here, everything works fine expect the handler creation sr->SpeechRecognized += gcnew System::EventHandler<System::Speech::Recognition::SpeechRecognizedEventArgs>(this,&Form1::sr_SpeechRecognized);在这里,一切正常,除了处理程序创建sr->SpeechRecognized += gcnew System::EventHandler<System::Speech::Recognition::SpeechRecognizedEventArgs>(this,&Form1::sr_SpeechRecognized);

If you comment out this handler part, everything will be OK.如果你注释掉这个处理程序部分,一切都会好的。 Here the Form means the current GUI form, the default GUI form built by the C++/CLI.这里的Form表示当前的 GUI 表单,即 C++/CLI 构建的默认 GUI 表单。 All these code is inside that form.所有这些代码都在该表单中。 I created this handler in the way I have read in an article.我按照我在一篇文章中读到的方式创建了这个处理程序。 What can I try next?我接下来可以尝试什么?

You're missing a ^ .你缺少一个^

sr->SpeechRecognized += gcnew System::EventHandler<System::Speech::Recognition::SpeechRecognizedEventArgs^>(this,&Form1::sr_SpeechRecognized);
                                                                                           // right here ^

Take a closer look at the error message you received, with the namespaces removed, and with a line break to make things line up.仔细查看您收到的错误消息,其中删除了命名空间,并使用换行符使事情对齐。

error C3352: 'void SpeechTest::Form1::sr_SpeechRecognized(Object^,SpeechRecognizedEventArgs^)' : 
the specified function does not match the delegate type 'void (Object^,SpeechRecognizedEventArgs)'
                                                                                                ^

You're trying to create a delegate to a method that takes a SpeechRecognizedEventArgs , but you're giving it the name of a method that takes a SpeechRecognizedEventArgs^ .您正在尝试为采用SpeechRecognizedEventArgs的方法创建委托,但您为其指定了采用SpeechRecognizedEventArgs^的方法的名称。

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

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