SpeechRecognitionEngine.EmulateRecognizeCompleted 事件

定义

SpeechRecognitionEngine 完成模拟输入的异步标识操作的时候引发。

public:
 event EventHandler<System::Speech::Recognition::EmulateRecognizeCompletedEventArgs ^> ^ EmulateRecognizeCompleted;
public event EventHandler<System.Speech.Recognition.EmulateRecognizeCompletedEventArgs> EmulateRecognizeCompleted;
member this.EmulateRecognizeCompleted : EventHandler<System.Speech.Recognition.EmulateRecognizeCompletedEventArgs> 
Public Custom Event EmulateRecognizeCompleted As EventHandler(Of EmulateRecognizeCompletedEventArgs) 
Public Event EmulateRecognizeCompleted As EventHandler(Of EmulateRecognizeCompletedEventArgs) 

事件类型

示例

以下示例是控制台应用程序的一部分,该应用程序加载语音识别语法并演示异步仿真输入、关联的识别结果以及语音识别器引发的关联事件。

using System;  
using System.Speech.Recognition;  
using System.Threading;  

namespace InProcessRecognizer  
{  
  class Program  
  {  
    // Indicate whether the asynchronous emulate recognition  
    // operation has completed.  
    static bool completed;  

    static void Main(string[] args)  
    {  

      // Initialize an instance of an in-process recognizer.  
      using (SpeechRecognitionEngine recognizer =   
        new SpeechRecognitionEngine(new System.Globalization.CultureInfo("en-US")))  
      {  
        // Create and load a sample grammar.  
        Grammar testGrammar =  
          new Grammar(new GrammarBuilder("testing testing"));  
        testGrammar.Name = "Test Grammar";  
        recognizer.LoadGrammar(testGrammar);  

        // Attach event handlers for recognition events.  
        recognizer.SpeechRecognized +=  
          new EventHandler<SpeechRecognizedEventArgs>(SpeechRecognizedHandler);  
        recognizer.EmulateRecognizeCompleted +=  
          new EventHandler<EmulateRecognizeCompletedEventArgs>(  
            EmulateRecognizeCompletedHandler);  

        completed = false;  

        // This EmulateRecognizeAsync call matches the grammar  
        // and generates a SpeechRecognized event.  
        recognizer.EmulateRecognizeAsync("testing testing");  

        // Wait for the asynchronous operation to complete.  
        while (!completed)  
        {  
          Thread.Sleep(333);  
        }  

        completed = false;  

        // This EmulateRecognizeAsync call does not match the grammar  
        // or generate a SpeechRecognized event.  
        recognizer.EmulateRecognizeAsync("testing one two three");  

        // Wait for the asynchronous operation to complete.  
        while (!completed)  
        {  
          Thread.Sleep(333);  
        }  
      }  

      Console.WriteLine();  
      Console.WriteLine("Press any key to exit...");  
      Console.ReadKey();  
    }  

    // Handle the SpeechRecognized event.  
    static void SpeechRecognizedHandler(  
      object sender, SpeechRecognizedEventArgs e)  
    {  
      if (e.Result != null)  
      {  
        Console.WriteLine("Result of 1st call to EmulateRecognizeAsync = {0}",  
          e.Result.Text ?? "<no text>");  
        Console.WriteLine();  
      }  
      else  
      {  
        Console.WriteLine("No recognition result");  
      }  
    }  

    // Handle the EmulateRecognizeCompleted event.  
    static void EmulateRecognizeCompletedHandler(  
      object sender, EmulateRecognizeCompletedEventArgs e)  
    {  
      if (e.Result == null)  
      {  
        Console.WriteLine("Result of 2nd call to EmulateRecognizeAsync = No result generated.");  
      }  

      // Indicate the asynchronous operation is complete.  
      completed = true;  
    }  
  }  
}  

注解

每个方法都会 EmulateRecognizeAsync 开始异步识别操作。 在 SpeechRecognitionEngine 完成异步操作时引发 EmulateRecognizeCompleted 事件。

EmulateRecognizeAsync 操作可以引发 SpeechDetectedSpeechHypothesizedSpeechRecognitionRejectedSpeechRecognized 事件。 事件 EmulateRecognizeCompleted 是识别器为给定操作引发的最后一个此类事件。

如果模拟识别成功,可以使用以下任一方法访问识别结果:

如果模拟识别不成功,则 SpeechRecognized 不会引发 事件,并且 Result 将为 null。

EmulateRecognizeCompletedEventArgs 派生自 AsyncCompletedEventArgs

SpeechRecognizedEventArgs 派生自 RecognitionEventArgs

创建 EmulateRecognizeCompleted 委托时,需要标识将要处理该事件的方法。 若要将事件与事件处理程序关联,请将该委托的一个实例添加到事件中。 除非移除了该委托,否则每当发生该事件时就会调用事件处理程序。 有关事件处理程序委托的详细信息,请参阅 事件和委托

适用于

另请参阅