SpeechRecognitionEngine.EmulateRecognizeCompleted 事件
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
当 SpeechRecognitionEngine 完成模拟输入的异步标识操作的时候引发。
public:
event EventHandler<System::Speech::Recognition::EmulateRecognizeCompletedEventArgs ^> ^ EmulateRecognizeCompleted;
public event EventHandler<System.Speech.Recognition.EmulateRecognizeCompletedEventArgs> EmulateRecognizeCompleted;
member this.EmulateRecognizeCompleted : EventHandler<System.Speech.Recognition.EmulateRecognizeCompletedEventArgs>
Public Custom Event EmulateRecognizeCompleted As EventHandler(Of EmulateRecognizeCompletedEventArgs)
Public Event EmulateRecognizeCompleted As EventHandler(Of EmulateRecognizeCompletedEventArgs)
事件类型
示例
以下示例是控制台应用程序的一部分,该应用程序加载语音识别语法并演示异步仿真输入、关联的识别结果以及语音识别器引发的关联事件。
using System;
using System.Speech.Recognition;
using System.Threading;
namespace InProcessRecognizer
{
class Program
{
// Indicate whether the asynchronous emulate recognition
// operation has completed.
static bool completed;
static void Main(string[] args)
{
// Initialize an instance of an in-process recognizer.
using (SpeechRecognitionEngine recognizer =
new SpeechRecognitionEngine(new System.Globalization.CultureInfo("en-US")))
{
// Create and load a sample grammar.
Grammar testGrammar =
new Grammar(new GrammarBuilder("testing testing"));
testGrammar.Name = "Test Grammar";
recognizer.LoadGrammar(testGrammar);
// Attach event handlers for recognition events.
recognizer.SpeechRecognized +=
new EventHandler<SpeechRecognizedEventArgs>(SpeechRecognizedHandler);
recognizer.EmulateRecognizeCompleted +=
new EventHandler<EmulateRecognizeCompletedEventArgs>(
EmulateRecognizeCompletedHandler);
completed = false;
// This EmulateRecognizeAsync call matches the grammar
// and generates a SpeechRecognized event.
recognizer.EmulateRecognizeAsync("testing testing");
// Wait for the asynchronous operation to complete.
while (!completed)
{
Thread.Sleep(333);
}
completed = false;
// This EmulateRecognizeAsync call does not match the grammar
// or generate a SpeechRecognized event.
recognizer.EmulateRecognizeAsync("testing one two three");
// Wait for the asynchronous operation to complete.
while (!completed)
{
Thread.Sleep(333);
}
}
Console.WriteLine();
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
}
// Handle the SpeechRecognized event.
static void SpeechRecognizedHandler(
object sender, SpeechRecognizedEventArgs e)
{
if (e.Result != null)
{
Console.WriteLine("Result of 1st call to EmulateRecognizeAsync = {0}",
e.Result.Text ?? "<no text>");
Console.WriteLine();
}
else
{
Console.WriteLine("No recognition result");
}
}
// Handle the EmulateRecognizeCompleted event.
static void EmulateRecognizeCompletedHandler(
object sender, EmulateRecognizeCompletedEventArgs e)
{
if (e.Result == null)
{
Console.WriteLine("Result of 2nd call to EmulateRecognizeAsync = No result generated.");
}
// Indicate the asynchronous operation is complete.
completed = true;
}
}
}
注解
每个方法都会 EmulateRecognizeAsync 开始异步识别操作。 在 SpeechRecognitionEngine 完成异步操作时引发 EmulateRecognizeCompleted 事件。
该 EmulateRecognizeAsync 操作可以引发 SpeechDetected、 SpeechHypothesized、 SpeechRecognitionRejected和 SpeechRecognized 事件。 事件 EmulateRecognizeCompleted 是识别器为给定操作引发的最后一个此类事件。
如果模拟识别成功,可以使用以下任一方法访问识别结果:
事件的Result处理程序EmulateRecognizeCompleted中的 对象中的 属性EmulateRecognizeCompletedEventArgs。
Result SpeechRecognizedEventArgs事件的处理程序中的 对象中的 SpeechRecognized 属性。
如果模拟识别不成功,则 SpeechRecognized 不会引发 事件,并且 Result 将为 null。
EmulateRecognizeCompletedEventArgs 派生自 AsyncCompletedEventArgs。
SpeechRecognizedEventArgs 派生自 RecognitionEventArgs。
创建 EmulateRecognizeCompleted 委托时,需要标识将要处理该事件的方法。 若要将事件与事件处理程序关联,请将该委托的一个实例添加到事件中。 除非移除了该委托,否则每当发生该事件时就会调用事件处理程序。 有关事件处理程序委托的详细信息,请参阅 事件和委托。