SpeechRecognitionEngine.LoadGrammarCompleted 事件

定义

SpeechRecognitionEngine 完成 Grammar 对象的异步加载时候引发。Raised when the SpeechRecognitionEngine finishes the asynchronous loading of a Grammar object.

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

示例

下面的示例创建一个进程内语音识别器,然后创建两种类型的语法来识别特定单词并接受免费听写。The following example creates an in-process speech recognizer, and then creates two types of grammars for recognizing specific words and for accepting free dictation. 该示例从每Grammar个已完成的语音识别语法构造对象,然后将Grammar对象异步加载到该SpeechRecognitionEngine实例。The example constructs a Grammar object from each of the completed speech recognition grammars, then asynchronously loads the Grammar objects to the SpeechRecognitionEngine instance. 识别器的LoadGrammarCompletedSpeechRecognized事件的处理程序向Grammar控制台写入用于分别执行识别和识别结果文本的对象的名称。Handlers for the recognizer's LoadGrammarCompleted and SpeechRecognized events write to the console the name of the Grammar object that was used to perform the recognition and the text of the recognition result, respectively.

using System;  
using System.Speech.Recognition;  
  
namespace SampleRecognition  
{  
  class Program  
  {  
    private static SpeechRecognitionEngine recognizer;  
    public static void Main(string[] args)  
    {  
  
      // Initialize an in-process speech recognition engine and set its input.  
      recognizer = new SpeechRecognitionEngine();  
      recognizer.SetInputToDefaultAudioDevice();  
  
      // Add a handler for the LoadGrammarCompleted event.  
      recognizer.LoadGrammarCompleted +=  
        new EventHandler<LoadGrammarCompletedEventArgs>(recognizer_LoadGrammarCompleted);  
  
      // Add a handler for the SpeechRecognized event.  
      recognizer.SpeechRecognized +=  
        new EventHandler<SpeechRecognizedEventArgs>(recognizer_SpeechRecognized);  
  
      // Create the "yesno" grammar.  
      Choices yesChoices = new Choices(new string[] { "yes", "yup", "yeah" });  
      SemanticResultValue yesValue =  
          new SemanticResultValue(yesChoices, (bool)true);  
      Choices noChoices = new Choices(new string[] { "no", "nope", "neah" });  
      SemanticResultValue noValue =  
          new SemanticResultValue(noChoices, (bool)false);  
      SemanticResultKey yesNoKey =  
          new SemanticResultKey("yesno", new Choices(new GrammarBuilder[] { yesValue, noValue }));  
      Grammar yesnoGrammar = new Grammar(yesNoKey);  
      yesnoGrammar.Name = "yesNo";  
  
      // Create the "done" grammar.  
      Grammar doneGrammar =  
        new Grammar(new Choices(new string[] { "done", "exit", "quit", "stop" }));  
      doneGrammar.Name = "Done";  
  
      // Create a dictation grammar.  
      Grammar dictation = new DictationGrammar();  
      dictation.Name = "Dictation";  
  
      // Load grammars to the recognizer.  
      recognizer.LoadGrammarAsync(yesnoGrammar);  
      recognizer.LoadGrammarAsync(doneGrammar);  
      recognizer.LoadGrammarAsync(dictation);  
  
      // Start asynchronous, continuous recognition.  
      recognizer.RecognizeAsync(RecognizeMode.Multiple);  
  
      // Keep the console window open.  
      Console.ReadLine();  
    }  
  
    // Handle the LoadGrammarCompleted event.   
    static void recognizer_LoadGrammarCompleted(object sender, LoadGrammarCompletedEventArgs e)  
    {  
      string grammarName = e.Grammar.Name;  
      bool grammarLoaded = e.Grammar.Loaded;  
  
      if (e.Error != null)  
      {  
        Console.WriteLine("LoadGrammar for {0} failed with a {1}.",  
        grammarName, e.Error.GetType().Name);  
  
        // Add exception handling code here.  
      }  
  
      Console.WriteLine("Grammar {0} {1} loaded.",  
      grammarName, (grammarLoaded) ? "is" : "is not");  
    }  
  
    // Handle the SpeechRecognized event.  
    static void recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)  
    {  
      Console.WriteLine("Grammar({0}): {1}", e.Result.Grammar.Name, e.Result.Text);  
  
      // Add event handler code here.  
    }  
  }  
}  
  

注解

识别器的LoadGrammarAsync方法启动异步操作。The recognizer's LoadGrammarAsync method initiates an asynchronous operation. 完成SpeechRecognitionEngine操作后,将引发此事件。The SpeechRecognitionEngine raises this event when it completes the operation. 若要获取Grammar识别器加载的对象,请Grammar使用关联LoadGrammarCompletedEventArgs的的属性。To get the Grammar object that the recognizer loaded, use the Grammar property of the associated LoadGrammarCompletedEventArgs. 若要获取识别Grammar器已加载的当前对象,请使用识别Grammars器的属性。To get the current Grammar objects the recognizer has loaded, use the recognizer's Grammars property.

如果识别器正在运行,则应用程序RequestRecognizerUpdate必须使用在加载、卸载、启用或禁用语法之前暂停语音识别引擎。If the recognizer is running, applications must use RequestRecognizerUpdate to pause the speech recognition engine before loading, unloading, enabling, or disabling a grammar.

创建 LoadGrammarCompleted 委托时,需要标识将处理该事件的方法。When you create a LoadGrammarCompleted delegate, you identify the method that will handle the event. 若要将事件与事件处理程序关联,请将该委托的一个实例添加到事件中。To associate the event with your event handler, add an instance of the delegate to the event. 除非移除了该委托,否则每当发生该事件时就会调用事件处理程序。The event handler is called whenever the event occurs, unless you remove the delegate. 有关事件处理程序委托的详细信息,请参阅事件和委托For more information about event-handler delegates, see Events and Delegates.

适用于

另请参阅