StateChangedEventArgs 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
从 StateChanged 事件返回数据。
public ref class StateChangedEventArgs : EventArgs
public class StateChangedEventArgs : EventArgs
type StateChangedEventArgs = class
inherit EventArgs
Public Class StateChangedEventArgs
Inherits EventArgs
- 继承
示例
以下示例创建一个共享语音识别器,然后创建两种类型的语法来识别特定单词和接受自由听写。 该示例以异步方式将所有创建的语法加载到识别器。 事件的处理程序 StateChanged 使用 EmulateRecognizeAsync 方法将 Windows 识别置于“侦听”模式。
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 "yesno" grammar.
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 "done" grammar.
Grammar doneGrammar =
new Grammar(new Choices(new string[] { "done", "exit", "quit", "stop" }));
doneGrammar.Name = "Done";
// Create 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();
}
// 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");
}
}
// Write the grammar name and the text of the recognized phrase to the console.
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");
}
}
}
注解
事件 StateChanged 由 SpeechRecognizer 类引发。 StateChangedEventArgs 派生自 EventArgs ,并传递给事件的处理程序 StateChanged 。
State 是一个只读属性。 无法以编程方式更改共享语音识别器的状态。 用户可以使用语音识别用户界面 (UI) 或通过 Windows 控制面板 的语音识别成员更改共享语音识别器的状态。
语音识别 UI 中的 “开” 和“ 睡眠” 设置都对应于 状态 Listening
。 语音识别 UI 中的 “关闭” 设置对应于 Stopped。
属性
RecognizerState |
获取 Windows 中的共享语音识别引擎的当前状态。 |
方法
Equals(Object) |
确定指定对象是否等于当前对象。 (继承自 Object) |
GetHashCode() |
作为默认哈希函数。 (继承自 Object) |
GetType() |
获取当前实例的 Type。 (继承自 Object) |
MemberwiseClone() |
创建当前 Object 的浅表副本。 (继承自 Object) |
ToString() |
返回表示当前对象的字符串。 (继承自 Object) |