如何在可执行的 c# WinForms 应用程序中构造语音识别代码?

How to structure speech recognition code in an executable c# WinForms App?

link to original code - please look at it first.

我不确定如何编写这段代码才能使其正常运行。到目前为止我的尝试:

网站中的代码使用 Microsoft 的系统语音识别从麦克风录制音频并将其转换为文本。除了,我不知道如何正确格式化网站上的代码。下面肯定不行。我到处都有红色下划线。我也不确定 'event-handler' 代码应该是什么样子。

using System;
using System.Collections.Generic;
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.Speech.Recognition;

namespace SystemSpeechRecognition_winForms
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }


    private void button2_Click(object sender, EventArgs e)
    {
        SpeechRecognitionEngine _speechRecognitionEngine = new SpeechRecognitionEngine();

        _speechRecognitionEngine.SetInputToDefaultAudioDevice();

        DictationGrammar _dictationGrammar = new DictationGrammar();

        _speechRecognitionEngine.LoadGrammar(_dictationGrammar);

        _speechRecognitionEngine.RecognizeAsync(RecognizeMode.Multiple);

ERROR for the four lines This is the error: . "delegate System.EventHandler" "Represents the method that will handle an event that has no event data" "Error: No overload for 'SpeechRecognized' matches delegate 'System.EventHanndler'

        _speechRecognitionEngine.SpeechRecognized -= new EventHandler(SpeechRecognized);



        _speechRecognitionEngine.SpeechHypothesized -= new EventHandler(SpeechHypothesizing);


        _speechRecognitionEngine.SpeechRecognized += new EventHandler(SpeechRecognized);

        _speechRecognitionEngine.SpeechHypothesized += new EventHandler(SpeechHypothesizing);

    }

    private void SpeechHypothesizing(object sender, SpeechHypothesizedEventArgs e)
    {

        ///real-time results from the engine

        string realTimeResults = e.Result.Text;

    }


    private void SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
    {

        ///final answer from the engine

        string finalAnswer = e.Result.Text;

    }

}

您遇到编译问题是因为 - 以其中一个事件为例 - SpeechRecognized 事件的类型为 EventHandler<SpeechRecognizedEventArgs> 并且您正试图为其分配一个非-通用 EventHandler class.

_speechRecognitionEngine.SpeechRecognized -= new EventHandler<SpeechRecognizedEventArgs>(SpeechRecognized);