共用方式為


LoadGrammarCompletedEventArgs 類別

定義

提供 SpeechRecognizerSpeechRecognitionEngine 物件的 LoadGrammarCompleted 事件資料。

public ref class LoadGrammarCompletedEventArgs : System::ComponentModel::AsyncCompletedEventArgs
public class LoadGrammarCompletedEventArgs : System.ComponentModel.AsyncCompletedEventArgs
type LoadGrammarCompletedEventArgs = class
    inherit AsyncCompletedEventArgs
Public Class LoadGrammarCompletedEventArgs
Inherits AsyncCompletedEventArgs
繼承
LoadGrammarCompletedEventArgs

範例

下列範例會建立共用語音辨識器,然後建立兩種類型的文法來辨識特定單字,以及接受自由聽寫。 此範例會以非同步方式將所有建立的文法載入至辨識器。 辨識器的 LoadGrammarCompletedSpeechRecognized 事件的處理常式會報告辨識的結果,以及用來執行辨識的文法。

using System;  
using System.Speech.Recognition;  

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

      // Initialize a shared speech recognition engine.  
      recognizer = new SpeechRecognizer();  

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

        // Add a handler for the StateChanged event.  
        recognizer.StateChanged +=  
          new EventHandler<StateChangedEventArgs>(recognizer_StateChanged);  

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

        // Create a dictation grammar.  
        Grammar dictation = new DictationGrammar();  
        dictation.Name = "Dictation";  

        // Load grammars to the recognizer.  
        recognizer.LoadGrammarAsync(yesnoGrammar);  
        recognizer.LoadGrammarAsync(doneGrammar);  
        recognizer.LoadGrammarAsync(dictation);  

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

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

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

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

    // Put the shared speech recognizer into "listening" mode.   
    static void recognizer_StateChanged(object sender, StateChangedEventArgs e)  
    {  
      if (e.RecognizerState != RecognizerState.Stopped)  
      {  
        recognizer.EmulateRecognizeAsync("Start listening");  
      }  
    }  
  }  
}  

備註

當 物件引發 其 SpeechRecognitionEngine.LoadGrammarCompletedSpeechRecognizer 物件引發其 LoadGrammarCompleted 事件時 SpeechRecognitionEngine ,就會建立 的 LoadGrammarCompletedEventArgs 實例。 呼叫 LoadGrammarAsync 方法完成時會引發事件。

若要取得已載入之物件的相關資訊 Grammar ,請存取 Grammar 事件處理常式中的 屬性。

如果辨識器在作業期間遇到例外狀況,屬性 Error 會設定為例外狀況,而 Loaded 相關聯的 Grammar 屬性可能是 false

屬性

Cancelled

取得值,指出非同步作業是否已取消。

(繼承來源 AsyncCompletedEventArgs)
Error

取得值,指出非同步作業期間是否發生錯誤。

(繼承來源 AsyncCompletedEventArgs)
Grammar

已完成載入的 Grammar 物件。

UserState

取得非同步工作的唯一識別項。

(繼承來源 AsyncCompletedEventArgs)

方法

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
RaiseExceptionIfNecessary()

如果非同步作業失敗,引發使用者提供的例外狀況。

(繼承來源 AsyncCompletedEventArgs)
ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

適用於

另請參閱