SpeechRecognizer.LoadGrammarCompleted Evento

Definição

Ocorre quando o reconhecedor finaliza o carregamento assíncrono de uma gramática de reconhecimento de fala.Occurs when the recognizer finishes the asynchronous loading of a speech recognition grammar.

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) 

Tipo de evento

EventHandler<LoadGrammarCompletedEventArgs>

Exemplos

O exemplo a seguir cria um reconhecedor de fala compartilhado e, em seguida, cria dois tipos de gramáticas para reconhecer palavras específicas e aceitar o ditado gratuito.The following example creates a shared speech recognizer, and then creates two types of grammars for recognizing specific words and for accepting free dictation. O exemplo carrega de forma assíncrona todas as gramáticas criadas para o reconhecedor.The example asynchronously loads all the created grammars to the recognizer. Os manipuladores para o reconhecedor LoadGrammarCompleted e os SpeechRecognized eventos gravam no console o nome da gramática usada para executar o reconhecimento e o texto do resultado do reconhecimento, respectivamente.Handlers for the recognizer's LoadGrammarCompleted and SpeechRecognized events write to the console the name of the grammar 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 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");  
      }  
    }  
  }  
}  

Comentários

O método do reconhecedor LoadGrammarAsync inicia uma operação assíncrona.The recognizer's LoadGrammarAsync method initiates an asynchronous operation. O reconhecedor gera o LoadGrammarCompleted evento quando conclui a operação.The recognizer raises the LoadGrammarCompleted event when it completes the operation. Para obter o Grammar objeto carregado pelo reconhecedor, use a Grammar Propriedade do associado LoadGrammarCompletedEventArgs .To get the Grammar object that the recognizer loaded, use the Grammar property of the associated LoadGrammarCompletedEventArgs. Para obter os Grammar objetos atuais que o reconhecedor carregou, use a propriedade do reconhecedor Grammars .To get the current Grammar objects the recognizer has loaded, use the recognizer's Grammars property.

Quando você cria um delegado para um LoadGrammarCompleted evento, identifica o método que manipulará o evento.When you create a delegate for a LoadGrammarCompleted event, you identify the method that will handle the event. Para associar o evento ao manipulador de eventos, adicione uma instância do delegado ao evento.To associate the event with your event handler, add an instance of the delegate to the event. O manipulador de eventos é chamado sempre que o evento ocorre, a menos que você remova o representante.The event handler is called whenever the event occurs, unless you remove the delegate. Para obter mais informações sobre delegados de manipulador de eventos, consulte eventos e delegados.For more information about event-handler delegates, see Events and Delegates.

Aplica-se a

Confira também