Bagikan melalui


SpeechRecognitionEngine.LoadGrammarCompleted Kejadian

Definisi

Dimunculkan ketika SpeechRecognitionEngine selesai memuat objek asinkron 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 Event LoadGrammarCompleted As EventHandler(Of LoadGrammarCompletedEventArgs) 
Public Custom Event LoadGrammarCompleted As EventHandler(Of LoadGrammarCompletedEventArgs) 

Jenis Acara

Contoh

Contoh berikut membuat pengenal ucapan dalam proses, lalu membuat dua jenis tata bahasa untuk mengenali kata-kata tertentu dan untuk menerima dikte gratis. Contoh membuat Grammar objek dari masing-masing tata bahasa pengenalan ucapan yang telah selesai, lalu secara asinkron memuat Grammar objek ke SpeechRecognitionEngine instans. Handler untuk recognizer LoadGrammarCompleted dan SpeechRecognized peristiwa menulis ke konsol nama Grammar objek yang digunakan untuk melakukan pengenalan dan teks hasil pengenalan, masing-masing.

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

Keterangan

Metode recognizer LoadGrammarAsync memulai operasi asinkron. meningkatkan SpeechRecognitionEngine peristiwa ini ketika menyelesaikan operasi. Untuk mendapatkan Grammar objek yang dimuat recognizer, gunakan Grammar properti dari yang terkait LoadGrammarCompletedEventArgs. Untuk mendapatkan objek saat ini Grammar yang telah dimuat recognizer, gunakan properti recognizer Grammars .

Jika recognizer berjalan, aplikasi harus menggunakan RequestRecognizerUpdate untuk menjeda mesin pengenalan ucapan sebelum memuat, membongkar, mengaktifkan, atau menonaktifkan tata bahasa.

Saat membuat LoadGrammarCompleted delegasi, Anda mengidentifikasi metode yang akan menangani peristiwa. Untuk mengaitkan peristiwa dengan penanganan aktivitas Anda, tambahkan instans delegasi ke peristiwa. Penanganan aktivitas dipanggil setiap kali peristiwa terjadi, kecuali Jika Anda menghapus delegasi. Untuk informasi selengkapnya tentang delegasi penanganan aktivitas, lihat Peristiwa dan Delegasi.

Berlaku untuk

Lihat juga