共用方式為


SpeechRecognizer.LoadGrammar(Grammar) 方法

定義

載入語音辨識文法。

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

參數

grammar
Grammar

要載入的語音辨識文法。

範例

下列範例是主控台應用程式的一部分,可載入語音辨識文法,並示範非同步模擬輸入、相關聯的辨識結果,以及語音辨識器所產生的相關事件。 如果 Windows 語音辨識未執行,則啟動此應用程式也會啟動 Windows 語音辨識。 如果 Windows 語音辨識處於 睡眠 狀態,則一律會傳回 EmulateRecognizeAsync null。

using System;  
using System.Speech.Recognition;  
using System.Threading;  

namespace SharedRecognizer  
{  
  class Program  
  {  
    // Indicate whether the asynchronous emulate recognition  
    // operation has completed.  
    static bool completed;  

    static void Main(string[] args)  
    {  
      // Initialize an instance of the shared recognizer.  
      using (SpeechRecognizer recognizer = new SpeechRecognizer())  
      {  
        // Create and load a sample grammar.  
        Grammar testGrammar =  
          new Grammar(new GrammarBuilder("testing testing"));  
        testGrammar.Name = "Test Grammar";  

        recognizer.LoadGrammar(testGrammar);  

        // Attach event handlers for recognition events.  
        recognizer.SpeechRecognized +=  
          new EventHandler<SpeechRecognizedEventArgs>(  
            SpeechRecognizedHandler);  
        recognizer.EmulateRecognizeCompleted +=  
          new EventHandler<EmulateRecognizeCompletedEventArgs>(  
            EmulateRecognizeCompletedHandler);  

        completed = false;  

        // This EmulateRecognizeAsync call generates a SpeechRecognized event.  
        recognizer.EmulateRecognizeAsync("testing testing");  

        // Wait for the asynchronous operation to complete.  
        while (!completed)  
        {  
          Thread.Sleep(333);  
        }  

        completed = false;  

        // This EmulateRecognizeAsync call does not match the grammar   
        // or generate a SpeechRecognized event.  
        recognizer.EmulateRecognizeAsync("testing one two three");  

        // Wait for the asynchronous operation to complete.  
        while (!completed)  
        {  
          Thread.Sleep(333);  
        }  
      }  

      Console.WriteLine();  
      Console.WriteLine("Press any key to exit...");  
      Console.ReadKey();  
    }  

    // Handle the SpeechRecognized event.  
    static void SpeechRecognizedHandler(  
      object sender, SpeechRecognizedEventArgs e)  
    {  
      if (e.Result != null)  
      {  
        Console.WriteLine("Recognition result = {0}",  
          e.Result.Text ?? "<no text>");  
      }  
      else  
      {  
        Console.WriteLine("No recognition result");  
      }  
    }   

    // Handle the EmulateRecognizeCompleted event.   
    static void EmulateRecognizeCompletedHandler(  
      object sender, EmulateRecognizeCompletedEventArgs e)  
    {  
      if (e.Result == null)  
      {  
        Console.WriteLine("No result generated.");  
      }  

      completed = true;  
    }  
  }  
}  

備註

如果已載入語音辨識文法、正在以非同步方式載入,或無法載入任何辨識器,則共用辨識器會擲回例外狀況。 如果辨識器正在執行,則應用程式必須 RequestRecognizerUpdate 在載入、卸載、啟用或停用文法之前,先使用暫停語音辨識引擎。

若要以非同步方式載入語音辨識文法,請使用 LoadGrammarAsync 方法。

適用於

另請參閱