Partager via


RecognizerUpdateReachedEventArgs.UserToken Propriété

Définition

Obtient le UserToken transmis au système lorsqu’une application appelle RequestRecognizerUpdate ou RequestRecognizerUpdate.

public:
 property System::Object ^ UserToken { System::Object ^ get(); };
public object UserToken { get; }
member this.UserToken : obj
Public ReadOnly Property UserToken As Object

Valeur de propriété

Retourne un objet qui contient .UserToken

Exemples

L’exemple suivant montre une application console qui charge et décharge des Grammar objets. L’application utilise la RequestRecognizerUpdate méthode pour demander au moteur de reconnaissance vocale de suspendre afin qu’il puisse recevoir une mise à jour. La méthode transmet un String objet pour le userToken paramètre qui décrit ce que l’application reconnaîtra après la mise à jour. L’application charge ou décharge ensuite un Grammar objet.

À chaque mise à jour, un gestionnaire d’événement SpeechRecognitionEngine.RecognizerUpdateReached écrit le contenu de userToken dans la console. À mesure que les grammaires sont chargées et déchargées, l’application reconnaît d’abord les noms des animaux de ferme, puis les noms des animaux de ferme et les noms des fruits, puis uniquement les noms des fruits.

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");  
    }  
  }  
}  

Remarques

Une application spécifie quand UserToken elle demande la génération d’un événement en appelant l’une RecognizerUpdateReachedSpeechRecognitionEngine.RequestRecognizerUpdate des méthodes ou SpeechRecognizer.RequestRecognizerUpdate qui prennent un userToken paramètre.

S’applique à

Voir aussi