Compartir a través de


Note

Please see Azure Cognitive Services for Speech documentation for the latest supported speech solutions.

LoadGrammarCompletedEventArgs Class

Provides data for the LoadGrammarCompleted event.

Inheritance Hierarchy

System.Object
  System.EventArgs
    System.ComponentModel.AsyncCompletedEventArgs
      Microsoft.Speech.Recognition.LoadGrammarCompletedEventArgs

Namespace:  Microsoft.Speech.Recognition
Assembly:  Microsoft.Speech (in Microsoft.Speech.dll)

Syntax

'Declaration
Public Class LoadGrammarCompletedEventArgs _
    Inherits AsyncCompletedEventArgs
'Usage
Dim instance As LoadGrammarCompletedEventArgs
public class LoadGrammarCompletedEventArgs : AsyncCompletedEventArgs

Remarks

An instance of LoadGrammarCompletedEventArgs is created when the SpeechRecognitionEngine object raises its LoadGrammarCompleted event or the DtmfRecognitionEngine object raises its LoadGrammarCompleted event. The events are raised when calls to the LoadGrammarAsync methods complete.

To obtain information about the Grammar object that was loaded, access the Grammar property in the handler for the event.

If the recognizer encounters an exception during the operation, the Error property is set to the exception and the Loaded property of the associated Grammar may be false.

Examples

The following example creates two speech recognition grammars and constructs a Grammar object from each of the completed grammars. It then asynchronously loads the Grammar objects to the SpeechRecognitionEngine instance. The handler for the recognizer's LoadGrammarCompleted event reports the status of Grammar objects when loaded. The handler for the SpeechRecognized event reports the name of the Grammar object that was used to perform a recognition and the text of the recognition result.

using System;
using Microsoft.Speech.Recognition;

namespace SampleRecognition
{
  class Program
  {
    private static SpeechRecognitionEngine recognizer;
    public static void Main(string[] args)
    {

      // Initialize a SpeechRecognitionEngine object and set its input.
      recognizer = new SpeechRecognitionEngine(new System.Globalization.CultureInfo("en-US"));
      recognizer.SetInputToDefaultAudioDevice();

      // 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);

      // Create the "yesno" grammar and build it into a Grammar object.
      Choices yesChoices = new Choices(new string[] { "yes", "yup", "yeah" });
      SemanticResultValue yesValue =
          new SemanticResultValue(yesChoices, (bool)true);
      Choices noChoices = new Choices(new string[] { "no", "nope", "neah" });
      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";

      // Load the Grammar objects to the recognizer.
      recognizer.LoadGrammarAsync(yesnoGrammar);
      recognizer.LoadGrammarAsync(doneGrammar);

      // Start asynchronous, continuous recognition.
      recognizer.RecognizeAsync(RecognizeMode.Multiple);

      // Keep the console window open.
      Console.ReadLine();
    }

    // Handle the LoadGrammarCompleted event. 
    static void recognizer_LoadGrammarCompleted(object sender, LoadGrammarCompletedEventArgs e)
    {
      string grammarName = e.Grammar.Name;
      bool grammarLoaded = e.Grammar.Loaded;
      bool grammarEnabled = e.Grammar.Enabled;

      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 and {2} enabled.", grammarName, (grammarLoaded) ? "is" : "is not", (grammarEnabled) ? "is" : "is not");
    }

    // 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.
    }
  }
}

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

See Also

Reference

LoadGrammarCompletedEventArgs Members

Microsoft.Speech.Recognition Namespace

Grammar

SpeechRecognitionEngine.LoadGrammar

SpeechRecognitionEngine.LoadGrammar

LoadGrammarAsync

DtmfRecognitionEngine