SpeechRecognizer.EmulateRecognizeCompleted 事件
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
当共享的识别器终结模拟输入的异步识别操作时发生。
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)
事件类型
示例
以下示例是控制台应用程序的一部分,该应用程序加载语音识别语法并演示异步模拟输入、关联的识别结果以及语音识别器引发的关联事件。 如果 Windows 语音识别未运行,则启动此应用程序也将启动 Windows 语音识别。 如果 Windows 语音识别处于 睡眠 模式,则 EmulateRecognizeAsync 始终返回 null。
using System;
using System.Speech.Recognition;
using System.Threading;
namespace SharedRecognizer
{
class Program
{
// Indicate whether the asynchronous emulate recognition
// operation has completed.
static bool completed;
static void Main(string[] args)
{
// Initialize an instance of the shared recognizer.
using (SpeechRecognizer recognizer = new SpeechRecognizer())
{
// 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 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("Recognition result = {0}",
e.Result.Text ?? "<no text>");
}
else
{
Console.WriteLine("No recognition result");
}
}
// Handle the EmulateRecognizeCompleted event.
static void EmulateRecognizeCompletedHandler(
object sender, EmulateRecognizeCompletedEventArgs e)
{
if (e.Result == null)
{
Console.WriteLine("No result generated.");
}
// Indicate the asynchronous operation is complete.
completed = true;
}
}
}
注解
每个方法都会 EmulateRecognizeAsync 开始异步识别操作。 识别器在 EmulateRecognizeCompleted
完成异步操作时引发 事件。
异步识别操作可以引发 SpeechDetected、、 SpeechHypothesizedSpeechRecognitionRejected和 SpeechRecognized 事件。 事件 EmulateRecognizeCompleted 是识别器为给定操作引发的最后一个此类事件。
为 EmulateRecognizeCompleted
事件创建委托时,需要标识将处理事件的方法。 若要将事件与事件处理程序关联,请将该委托的一个实例添加到事件中。 除非移除了该委托,否则每当发生该事件时就会调用事件处理程序。 有关事件处理程序委托的详细信息,请参阅 事件和委托。