LoadGrammarCompletedEventArgs 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
为 SpeechRecognizer 或 SpeechRecognitionEngine 对象的 LoadGrammarCompleted
事件提供数据。
public ref class LoadGrammarCompletedEventArgs : System::ComponentModel::AsyncCompletedEventArgs
public class LoadGrammarCompletedEventArgs : System.ComponentModel.AsyncCompletedEventArgs
type LoadGrammarCompletedEventArgs = class
inherit AsyncCompletedEventArgs
Public Class LoadGrammarCompletedEventArgs
Inherits AsyncCompletedEventArgs
- 继承
示例
以下示例创建一个共享语音识别器,然后创建两种类型的语法来识别特定单词和接受自由听写。 该示例以异步方式将所有创建的语法加载到识别器。 识别器和LoadGrammarCompletedSpeechRecognized事件的处理程序报告识别结果以及用于执行识别的语法。
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 the "yesno" grammar and build it into a Grammar object.
Choices yesChoices = new Choices(new string[] { "yes", "yup", "yah}" });
SemanticResultValue yesValue =
new SemanticResultValue(yesChoices, (bool)true);
Choices noChoices = new Choices(new string[] { "no", "nope", "nah" });
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 within the constructor of a Grammar object.
Grammar doneGrammar =
new Grammar(new GrammarBuilder(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);
// 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");
}
}
}
}
注解
当 对象引发其 SpeechRecognitionEngine.LoadGrammarCompleted 事件或 对象引发其 事件时SpeechRecognitionEngine,SpeechRecognizer将创建 的LoadGrammarCompleted实例LoadGrammarCompletedEventArgs
。 对方法的调用完成时, LoadGrammarAsync
将引发事件。
若要获取有关 Grammar 已加载的对象的信息,请在 事件的处理程序中访问 Grammar 属性。
如果识别器在操作期间遇到异常,则 Error 属性设置为异常, Loaded 并且关联的 Grammar 的 属性可以是 false
。
属性
Cancelled |
获取一个值,该值指示异步操作是否已被取消。 (继承自 AsyncCompletedEventArgs) |
Error |
获取一个值,该值指示异步操作期间发生的错误。 (继承自 AsyncCompletedEventArgs) |
Grammar |
完成加载的 Grammar 对象。 |
UserState |
获取异步任务的唯一标识符。 (继承自 AsyncCompletedEventArgs) |
方法
Equals(Object) |
确定指定对象是否等于当前对象。 (继承自 Object) |
GetHashCode() |
作为默认哈希函数。 (继承自 Object) |
GetType() |
获取当前实例的 Type。 (继承自 Object) |
MemberwiseClone() |
创建当前 Object 的浅表副本。 (继承自 Object) |
RaiseExceptionIfNecessary() |
如果异步操作失败,则引发用户提供的异常。 (继承自 AsyncCompletedEventArgs) |
ToString() |
返回表示当前对象的字符串。 (继承自 Object) |