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 Event LoadGrammarCompleted As EventHandler(Of LoadGrammarCompletedEventArgs) 
Public Custom 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 이벤트에 이벤트를 처리 하는 메서드를 식별 합니다. 이벤트를 이벤트 처리기와 연결하려면 대리자의 인스턴스를 해당 이벤트에 추가합니다. 대리자를 제거하지 않는 경우 이벤트가 발생할 때마다 이벤트 처리기가 호출됩니다. 이벤트 처리기 대리자에 대 한 자세한 내용은 참조 하세요. 이벤트 및 대리자합니다.

적용 대상

추가 정보