SpeechRecognizer.LoadGrammarCompleted 事件

定义

当识别器完成语音识别语法的异步加载时发生。

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) 

事件类型

示例

以下示例创建一个共享语音识别器,然后创建两种类型的语法来识别特定单词和接受自由听写。 该示例以异步方式将所有创建的语法加载到识别器。 识别器和LoadGrammarCompletedSpeechRecognized事件的处理程序分别将用于执行识别结果的语法名称和文本写入控制台。

using System;
using System.Speech.Recognition;

namespace SampleRecognition
{
  class Program
  {
    private static SpeechRecognizer recognizer;
    public static void Main(string[] args)
    {

      // Initialize a shared speech recognition engine.
      recognizer = new SpeechRecognizer();

        // 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);

        // Add a handler for the StateChanged event.
        recognizer.StateChanged +=
          new EventHandler<StateChangedEventArgs>(recognizer_StateChanged);

        // Create "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 "done" grammar.
        Grammar doneGrammar =
          new Grammar(new Choices(new string[] { "done", "exit", "quit", "stop" }));
        doneGrammar.Name = "Done";

        // Create dictation grammar.
        Grammar dictation = new DictationGrammar();
        dictation.Name = "Dictation";

        // Load grammars to the recognizer.
        recognizer.LoadGrammarAsync(yesnoGrammar);
        recognizer.LoadGrammarAsync(doneGrammar);
        recognizer.LoadGrammarAsync(dictation);

        // Keep the console window open.
        Console.ReadLine();
      }

    // 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.
    }

    // 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");
    }

    // Put the shared speech recognizer into "listening" mode.
    static void recognizer_StateChanged(object sender, StateChangedEventArgs e)
    {
      if (e.RecognizerState != RecognizerState.Stopped)
      {
        recognizer.EmulateRecognizeAsync("Start listening");
      }
    }
  }
}

注解

识别器 LoadGrammarAsync 的方法启动异步操作。 识别器在 LoadGrammarCompleted 完成操作时引发 事件。 若要获取 Grammar 识别器加载的对象,请使用 Grammar 关联的 LoadGrammarCompletedEventArgs的 属性。 若要获取识别器已加载的当前 Grammar 对象,请使用识别器的 Grammars 属性。

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

适用于

另请参阅