2016-08-15 106 views
1

我正在製作一個具有語音命令的簡單程序。我不完全知道vb.net中的代碼,所以我試圖複製C#中的代碼,然後把它放在VB中,當我運行它時,它說「空規則是不允許的」Visual Basic .Net語音命令

這是我的代碼:

Private Sub loginCommandFom_Load(sender As Object, e As EventArgs) Handles MyBase.Load 
    Try 
     Dim commandChoices As New Recognition.Choices 
     Dim grammarBuilder As New Recognition.GrammarBuilder 
     Dim gr As New Recognition.Grammar(grammarBuilder) 
     commandChoices.Add(New String("Hey", "Wazzup")) 
     grammarBuilder.Append(commandChoices) 
     commandRecognition.LoadGrammarAsync(gr) 
     commandRecognition.SetInputToDefaultAudioDevice() 
     commandRecognition.RecognizeAsync() 
    Catch ex As Exception 
     MessageBox.Show(ex.Message, "Error!", MessageBoxButtons.OK, MessageBoxIcon.Information) 
    End Try 
End Sub 
Private Sub speechCompleted(sender As Object, e As System.Speech.Recognition.RecognizeCompletedEventArgs) Handles commandRecognition.RecognizeCompleted 
    commandRecognition.RecognizeAsync() 
End Sub 
Private Sub speechRecognize(sender As Object, e As System.Speech.Recognition.SpeechRecognizedEventArgs) Handles commandRecognition.SpeechRecognized 
    Select Case e.Result.Text 
     Case "Hey" 
      MsgBox("Yeah") 
     Case "Wazzup" 
      MsgBox("Yah") 
    End Select 
End Sub 

回答

0

有你犯的一個小錯誤是你正在嘗試將null grammarbuilder添加到gramar中。

空規則是不允許的 - 在這裏規則意味着語法,當您使用語法classGrammar(grammarBuilder)的構造

化妝以下更改爲它分配它在當時是空的。

Private Sub loginCommandFom_Load(sender As Object, e As EventArgs) Handles MyBase.Load 
Try 
    Dim commandChoices As New Recognition.Choices 
    Dim grammarBuilder As New Recognition.GrammarBuilder 
    'removed 
    commandChoices.Add(New String("Hey", "Wazzup")) 
    grammarBuilder.Append(commandChoices) 
    'added 
    Dim gr As New Recognition.Grammar(grammarBuilder) 
    commandRecognition.LoadGrammarAsync(gr) 
    commandRecognition.SetInputToDefaultAudioDevice() 
    commandRecognition.RecognizeAsync() 
Catch ex As Exception 
    MessageBox.Show(ex.Message, "Error!", MessageBoxButtons.OK, MessageBoxIcon.Information) 
End Try 
End Sub 
Private Sub speechCompleted(sender As Object, e As System.Speech.Recognition.RecognizeCompletedEventArgs) Handles commandRecognition.RecognizeCompleted 
commandRecognition.RecognizeAsync() 
End Sub 
Private Sub speechRecognize(sender As Object, e As System.Speech.Recognition.SpeechRecognizedEventArgs) Handles commandRecognition.SpeechRecognized 
Select Case e.Result.Text 
    Case "Hey" 
     MsgBox("Yeah") 
    Case "Wazzup" 
     MsgBox("Yah") 
End Select 
End Sub