SpeechRecognitionEngine.LoadGrammarCompleted 事件
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
SpeechRecognitionEngine 完成 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)
事件類型
範例
下列範例會建立處理中的語音辨識器,然後建立兩種類型的文法來辨識特定單字,以及接受免費的聽寫。 此範例會 Grammar 從每個已完成的語音辨識文法建構 物件,然後以非同步方式將 Grammar 物件 SpeechRecognitionEngine 載入實例。 辨識器和 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 委派時,必須識別處理事件的方法。 若要使事件與您的事件處理常式產生關聯,請將委派的執行個體 (Instance) 加入至事件。 除非您移除委派,否則每當事件發生時就會呼叫事件處理常式。 如需事件處理常式委派的詳細資訊,請參閱 事件和委派。