2013-06-11 49 views
0

我在Visual Studio中創建應用程序,使用語音識別進行應用程序控制。最好的方法,如何分配語音命令的事件/方法在C#

我想問你如何將語音命令分配給方法的最佳方法。使用語法生成器和選擇

I'm:

//Create Grammar Builder with Choices 
GrammarBuilder slovnik = new GrammarBuilder(); 
slovnik.Append(new Choices("stop", "go")); 

而且如果我倒是喜歡一個字從選擇到(語音命令)來分配方法(例如 - 顯示消息框) - 在事件處理程序I'm如果使用命令:

void recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e) 
     { 
      if (e.Result.Text == "stop") 
      { 
       MessageBox.Show("Some message, that the voice command works"); 
      } 
     } 

我的問題是 - 是如果條款如何從語法加盟詞/短語方法/事件或者有沒有更好的(清潔劑)解決方案如何最好的辦法去做?我使用C#System.Speech.Recognition.SpeechRecognitionEngine

非常感謝!

回答

0

您可以使用custom attributesreflection

首先,創建自己的VoiceCommandAttribute派生自Attribute類。它應該有一個構造函數,它將命令名稱存儲在string字段中。命令名稱應該可以通過屬性訪問。

那麼當你的應用程序啓動,你可以遍歷所有/一些類和它們的方法,使用反射retrive它們的屬性,並作出解釋,其中鍵是從VoiceCommandAttributes命令名稱和值是從MethodInfos創建Delegates

最後,在SpeechRecognized事件處理程序中,您可以查找字典中的單詞/短語並調用委託。

祝你好運。

0

我認爲使用案例也可以做到這一點。

void recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e) 
     {  
    string speech = e.Result.Text;  
    switch(speech) 
    {    
    case: "stop":   
//do what you want   
    break;  
    }  
}