无法应用Operator + =

时间:2015-01-12 11:09:32

标签: c#

我正在创建一个C#WinForms应用程序,应该使用语音命令关闭应用程序"退出"。

然而它给了我一个例外:

  

运算符+ =无法应用于系统语音或主方法的操作数

在此代码中:

public partial class Form1 : Form
{
    SpeechRecognitionEngine sRecongize = new SpeechRecognitionEngine();

    private void Form1_Load(object sender, EventArgs e)
    {
        // Compiler error here:
        sRecongize += sRecongize_SpeechRecognized;
    }

    private void sRecongize_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
    {
        if (e.Result.Text == "exit")
        {
            Application.Exit();
        }
    }
}

我如何订阅活动?

1 个答案:

答案 0 :(得分:5)

您需要订阅特定事件,在本例中为SpeechRecognized,而不是整个班级:

sRecongize.SpeechRecognized += sRecongize_SpeechRecognized;