简体   繁体   中英

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);

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. 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^ .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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