RecognizerUpdateReachedEventArgs.UserToken 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
UserToken
取得應用程式呼叫 RequestRecognizerUpdate 或 RequestRecognizerUpdate 時傳遞至系統的 。
public:
property System::Object ^ UserToken { System::Object ^ get(); };
public object UserToken { get; }
member this.UserToken : obj
Public ReadOnly Property UserToken As Object
屬性值
傳回包含 的物件 UserToken
。
範例
下列範例顯示載入和卸載 Grammar 物件的主控台應用程式。 應用程式會 RequestRecognizerUpdate 使用 方法來要求語音辨識引擎暫停,以便接收更新。 方法會傳入 String 參數的 userToken
物件,描述應用程式在更新之後將辨識的內容。 應用程式接著會載入或卸載 Grammar 物件。
在每個更新時,事件的處理常式 SpeechRecognitionEngine.RecognizerUpdateReached 會將 的內容 userToken
寫入主控台。 當載入和卸載文法時,應用程式會先辨識伺服器陣列動物的名稱、伺服器陣列動物的名稱和動物的名稱,然後只辨識植物的名稱。
using System;
using System.Speech.Recognition;
using System.Collections.Generic;
using System.Threading;
namespace SampleRecognition
{
class Program
{
private static SpeechRecognitionEngine recognizer;
public static void Main(string[] args)
{
// Initialize an in-process speech recognition engine and configure its input.
using (recognizer = new SpeechRecognitionEngine(
new System.Globalization.CultureInfo("en-US")))
{
recognizer.SetInputToDefaultAudioDevice();
// Create the first grammar - Farm.
Choices animals = new Choices(new string[] { "cow", "pig", "goat" });
GrammarBuilder farm = new GrammarBuilder(animals);
Grammar farmAnimals = new Grammar(farm);
farmAnimals.Name = "Farm";
// Create the second grammar - Fruit.
Choices fruit = new Choices(new string[] { "apples", "peaches", "oranges" });
GrammarBuilder favorite = new GrammarBuilder(fruit);
Grammar favoriteFruit = new Grammar(favorite);
favoriteFruit.Name = "Fruit";
// Attach event handlers.
recognizer.SpeechRecognized +=
new EventHandler<SpeechRecognizedEventArgs>(recognizer_SpeechRecognized);
recognizer.RecognizerUpdateReached +=
new EventHandler<RecognizerUpdateReachedEventArgs>(recognizer_RecognizerUpdateReached);
recognizer.SpeechRecognitionRejected +=
new EventHandler<SpeechRecognitionRejectedEventArgs>(recognizer_SpeechRecognitionRejected);
// Load the farmAnimals grammar
recognizer.LoadGrammar(farmAnimals);
// Start continuous, asynchronous recognition.
recognizer.RecognizeAsync(RecognizeMode.Multiple);
Console.WriteLine("Starting asynchronous recognition...");
Console.WriteLine(" Farm animals will now be recognized.");
Thread.Sleep(7000);
Console.WriteLine();
// Load the Fruit grammar.
string activeGrammars = "Farm animals and fruits will now be recognized.";
recognizer.RequestRecognizerUpdate(activeGrammars);
recognizer.LoadGrammarAsync(favoriteFruit);
Console.WriteLine();
Thread.Sleep(7000);
Console.WriteLine();
// Unload the Farm grammar.
string onlyFruit = "Only fruits will now be recognized.";
recognizer.RequestRecognizerUpdate(onlyFruit);
recognizer.UnloadGrammar(farmAnimals);
Thread.Sleep(7000);
}
// Keep the console window open.
Console.WriteLine();
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
}
// At the update, describe what the application will recognize next.
public static void recognizer_RecognizerUpdateReached(object sender, RecognizerUpdateReachedEventArgs e)
{
Console.WriteLine(" Update reached: " + e.UserToken);
}
// Write the text of the recognized phrase to the console.
static void recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
Console.WriteLine(" Speech recognized: " + e.Result.Text);
}
// Write a message to the console when recognition fails.
static void recognizer_SpeechRecognitionRejected(object sender, SpeechRecognitionRejectedEventArgs e)
{
Console.WriteLine(" Recognition attempt failed");
}
}
}
備註
應用程式會 UserToken
藉由呼叫採用 參數的其中 SpeechRecognitionEngine.RequestRecognizerUpdate 一個 或 SpeechRecognizer.RequestRecognizerUpdate 方法 userToken
,指定何時要求產生 RecognizerUpdateReached
事件。