【问题标题】:How to create C# event handlers in C++/CLI?如何在 C++/CLI 中创建 C# 事件处理程序?
【发布时间】:2013-05-12 05:12:06
【问题描述】:

这是我的代码:

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)
{
 }

此代码产生以下错误

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 ==========

在这里,一切正常,期待处理程序创建sr-&gt;SpeechRecognized += gcnew System::EventHandler&lt;System::Speech::Recognition::SpeechRecognizedEventArgs&gt;(this,&amp;Form1::sr_SpeechRecognized);

如果您注释掉这个处理程序部分,一切都会好起来的。这里Form 表示当前的GUI 表单,由C++/CLI 构建的默认GUI 表单。所有这些代码都在该表单中。我按照我在文章中读到的方式创建了这个处理程序。接下来我可以尝试什么?

【问题讨论】:

    标签: c# .net visual-studio-2010 c++-cli speech-recognition


    【解决方案1】:

    您缺少^

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

    仔细查看您收到的错误消息,删除了命名空间,并使用换行符使内容对齐。

    错误 C3352:“无效 SpeechTest::Form1::sr_SpeechRecognized(Object^,SpeechRecognizedEventArgs^)”: 指定的函数与委托类型“void (Object^,SpeechRecognizedEventArgs)”不匹配 ^

    您正在尝试为采用 SpeechRecognizedEventArgs 的方法创建一个委托,但您为其提供了采用 SpeechRecognizedEventArgs^ 的方法的名称。

    【讨论】:

      猜你喜欢
      • 2016-01-05
      • 1970-01-01
      • 2013-12-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-05
      • 2015-01-17
      • 2010-09-25
      相关资源
      最近更新 更多