SpeechRecognitionEngine.LoadGrammarCompleted 이벤트

정의

SpeechRecognitionEngineGrammar 개체의 비동기 로딩을 완료했을 때 발생했습니다.

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 Event LoadGrammarCompleted As EventHandler(Of LoadGrammarCompletedEventArgs) 
Public Custom Event LoadGrammarCompleted As EventHandler(Of LoadGrammarCompletedEventArgs) 

이벤트 유형

예제

다음 예제에서는 In-Process 음성 인식기를 만든 다음 특정 단어를 인식하고 자유 받아쓰기를 수락하기 위한 두 가지 유형의 문법을 만듭니다. 이 예제에서는 완료된 각 음성 인식 문법에서 개체를 생성 Grammar 한 다음 개체를 instance 비동기적으로 로드합니다 GrammarSpeechRecognitionEngine . 인식기 LoadGrammarCompletedSpeechRecognized 이벤트에 대한 처리기는 각각 인식 결과 및 인식을 수행하는 데 사용된 개체의 Grammar 이름을 콘솔에 씁니다.

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 메서드는 비동기 작업을 시작 합니다. 는 SpeechRecognitionEngine 작업을 완료할 때 이 이벤트를 발생합니다. 가져오려는 합니다 Grammar 인식기에 로드 된 개체를 사용 하 여는 Grammar 속성은 연결 된 LoadGrammarCompletedEventArgs합니다. 현재 가져오려는 Grammar 인식기를 사용 하는 인식기를 로드 하는 개체 Grammars 속성입니다.

인식기가 실행 중인 경우 애플리케이션은 를 사용하여 RequestRecognizerUpdate 문법을 로드, 언로드, 사용 또는 사용하지 않도록 설정하기 전에 음성 인식 엔진을 일시 중지해야 합니다.

LoadGrammarCompleted 대리자를 만들 때, 이벤트를 처리할 메서드를 식별합니다. 이벤트를 이벤트 처리기와 연결하려면 대리자의 인스턴스를 해당 이벤트에 추가합니다. 대리자를 제거하지 않는 경우 이벤트가 발생할 때마다 이벤트 처리기가 호출됩니다. 이벤트 처리기 대리자에 대 한 자세한 내용은 참조 하세요. 이벤트 및 대리자합니다.

적용 대상

추가 정보