SpeechRecognitionEngine.UnloadGrammar(Grammar) 方法

定义

从指定的 Grammar 实例卸载一个指定的 SpeechRecognitionEngine 对象。Unloads a specified Grammar object from the SpeechRecognitionEngine instance.

public:
 void UnloadGrammar(System::Speech::Recognition::Grammar ^ grammar);
public void UnloadGrammar (System.Speech.Recognition.Grammar grammar);
member this.UnloadGrammar : System.Speech.Recognition.Grammar -> unit

参数

grammar
Grammar

未加载的语法对象。The grammar object to unload.

例外

GrammarnullGrammar is null.

此语法识别器未加载,或者此识别器当前加载的语法异步。The grammar is not loaded in this recognizer, or this recognizer is currently loading the grammar asynchronously.

示例

下面的示例演示了控制台应用程序的一部分,该应用程序演示了语音识别语法的同步加载和卸载。The following example shows part of a console application that demonstrates the synchronous loading and unloading of speech recognition grammars.

Loading grammars...  
Loaded grammars:  
 - Grammar1  
 - Grammar2  
 - Grammar3  
  
Unloading Grammar1...  
Loaded grammars:  
 - Grammar2  
 - Grammar3  
  
Unloading all grammars...  
No grammars loaded.  
  
Press any key to exit...  
  
using System;  
using System.Collections.Generic;  
using System.Globalization;  
using System.Speech.Recognition;  
  
namespace UnloadGrammars  
{  
  class Program  
  {  
    static void Main(string[] args)  
    {  
      using (SpeechRecognitionEngine recognizer =  
        new SpeechRecognitionEngine(new CultureInfo("en-US")))  
      {  
        Console.WriteLine("Loading grammars...");  
  
        // Create and load a number of grammars.  
        Grammar grammar1 = new Grammar(new GrammarBuilder("first grammar"));  
        grammar1.Name = "Grammar1";  
        recognizer.LoadGrammar(grammar1);  
  
        Grammar grammar2 = new Grammar(new GrammarBuilder("second grammar"));  
        grammar2.Name = "Grammar2";  
        recognizer.LoadGrammar(grammar2);  
  
        Grammar grammar3 = new Grammar(new GrammarBuilder("third grammar"));  
        grammar3.Name = "Grammar3";  
        recognizer.LoadGrammar(grammar3);  
  
        // List the recognizer's loaded grammars.  
        ListGrammars(recognizer);  
  
        // Unload one grammar and list the loaded grammars.  
        Console.WriteLine("Unloading Grammar1...");  
        recognizer.UnloadGrammar(grammar1);  
        ListGrammars(recognizer);  
  
        // Unload all grammars and list the loaded grammars.  
        Console.WriteLine("Unloading all grammars...");  
        recognizer.UnloadAllGrammars();  
        ListGrammars(recognizer);  
      }  
  
      Console.WriteLine("Press any key to exit...");  
      Console.ReadKey();  
    }  
  
    private static void ListGrammars(SpeechRecognitionEngine recognizer)  
    {  
      // Make a copy of the recognizer's grammar collection.  
      List<Grammar> loadedGrammars = new List<Grammar>(recognizer.Grammars);  
  
      if (loadedGrammars.Count > 0)  
      {  
        Console.WriteLine("Loaded grammars:");  
        foreach (Grammar g in recognizer.Grammars)  
        {  
          Console.WriteLine(" - {0}", g.Name);  
        }  
      }  
      else  
      {  
        Console.WriteLine("No grammars loaded.");  
      }  
      Console.WriteLine();  
    }  
  }  
}  

注解

如果识别器正在运行,则应用程序RequestRecognizerUpdate必须先使用SpeechRecognitionEngine暂停实例,然后再加载、卸载、 Grammar启用或禁用对象。If the recognizer is running, applications must use RequestRecognizerUpdate to pause the SpeechRecognitionEngine instance before loading, unloading, enabling, or disabling a Grammar object. 若要卸载Grammar所有对象,请UnloadAllGrammars使用方法。To unload all Grammar objects, use the UnloadAllGrammars method.

适用于

另请参阅