共用方式為


SpeechRecognitionEngine.UnloadGrammar(Grammar) 方法

定義

Grammar 執行個體卸載指定的 SpeechRecognitionEngine 物件。

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

參數

grammar
Grammar

要卸載的文法物件。

例外狀況

Grammarnull

這個辨識器未將文法載入,或這個辨識器正在以非同步方式載入文法。

範例

下列範例顯示主控台應用程式的一部分,示範語音辨識文法的同步載入和卸載。

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

備註

如果辨識器正在執行,應用程式必須在載入、卸載、啟用或停用 Grammar 物件之前,先使用 RequestRecognizerUpdate 暫停 SpeechRecognitionEngine 實例。 若要卸載所有 Grammar 物件,請使用 UnloadAllGrammars 方法。

適用於

另請參閱