Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在for循环中添加用于语音识别的单词_C#_Wpf_Grammar_Speech Recognition - Fatal编程技术网

C# 在for循环中添加用于语音识别的单词

C# 在for循环中添加用于语音识别的单词,c#,wpf,grammar,speech-recognition,C#,Wpf,Grammar,Speech Recognition,我正在尝试从datagridview在GrammarBuilder中添加单词 List<string> grammerList = new List<string>(); public myForm() { InitializeComponent(); recognizer = new SpeechRecognitionEngine(); InitializeSpeechRecognition

我正在尝试从datagridview在GrammarBuilder中添加单词

List<string> grammerList = new List<string>();                           

public myForm()
{
    InitializeComponent();
    recognizer = new SpeechRecognitionEngine();
    InitializeSpeechRecognitionEngine();
}

private void InitializeSpeechRecognitionEngine()
{
    recognizer.SetInputToDefaultAudioDevice();
    Grammar customGrammar = CreateCustomGrammar();
    recognizer.LoadGrammar(customGrammar);
    recognizer.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(
        recognizer_SpeechRecognized);
}

private Grammar CreateCustomGrammar()
{
    DataTable myTable = new DataTable();
    OleDbConnection myConnection = new OleDbConnection(connection);
    OleDbCommand myCommand = new OleDbCommand("Select * From wordtable", myConnection);
    OleDbDataAdapter adapter = new OleDbDataAdapter(myCommand);
    adapter.Fill(myTable);

    for (ctr = 1; ctr < myTable.RowCount; ctr++)
        grammerList.Add(myTable.Rows[ctr].Cells[0].Value.ToString());

    Choices mychoices = new Choices(grammerList.ToArray());
    GrammarBuilder myGrammarBuilder = new GrammarBuilder(c);
    Grammar myGrammar = new Grammar(myGrammarBuilder);
    return myGrammar; 
}
识别引擎不同于识别器,对吗? 识别引擎是c的识别器,语音识别器是Windows的识别器,对吗?

试试

private Grammar CreateCustomGrammar()
{
    DataTable myTable = new DataTable();
    using ( OleDbConnection myConnection = new OleDbConnection(connection) )
    using ( OleDbCommand myCommand = new OleDbCommand("Select * From wordtable", myConnection) )
    {
    myConnection.Open();
    myTable.Load (myCommand.ExecuteReader();
    grammerList.AddRange ((from r in myTable.Rows select r[0]..ToString()).ToArray());
    }


   Choices mychoices = new Choices(grammerList.ToArray());
   GrammarBuilder myGrammarBuilder = new GrammarBuilder(c);
   Grammar myGrammar = new Grammar(myGrammarBuilder);
   return myGrammar; 
}

当您得到错误时,您是否检查了哪个变量为空?
private Grammar CreateCustomGrammar()
{
    DataTable myTable = new DataTable();
    using ( OleDbConnection myConnection = new OleDbConnection(connection) )
    using ( OleDbCommand myCommand = new OleDbCommand("Select * From wordtable", myConnection) )
    {
    myConnection.Open();
    myTable.Load (myCommand.ExecuteReader();
    grammerList.AddRange ((from r in myTable.Rows select r[0]..ToString()).ToArray());
    }


   Choices mychoices = new Choices(grammerList.ToArray());
   GrammarBuilder myGrammarBuilder = new GrammarBuilder(c);
   Grammar myGrammar = new Grammar(myGrammarBuilder);
   return myGrammar; 
}