[英]How can I change the value inside this array? C#
所以我想用一些可以更改的命令列表制作一些語音識別(使用system.speech.recognition)軟件。 因此,例如,起初您有兩種命令列表:“ Left,Right”,當您說左或右時它將執行某些功能。 但是我想在此軟件中可以隨時更改命令列表,例如,在上面的命令列表中,我想將單詞“ Left”更改為“ Up”,當我說“ Up”時,它將執行在執行“左”字之前的功能。 這是我的代碼:
Choices commands = new Choices();
GrammarBuilder gBuilder = new GrammarBuilder();
public void Masokey_Load(object sender, EventArgs e)
{
// Choices commands = new Choices();
commands.Add(new string[] { Atext.Text, Dtext.Text});
// GrammarBuilder gBuilder = new GrammarBuilder();
gBuilder.Append(commands);
Grammar grammar = new Grammar(gBuilder);
recEngine.LoadGrammarAsync(grammar);
recEngine.SetInputToDefaultAudioDevice();
recEngine.SpeechRecognized += RecEngine_SpeechRecognized;
}
//save_btn
public void Savebtn_Click(object sender, EventArgs e)
{
commands.Add(new string[] { Atext.Text, Dtext.Text});
gBuilder.Append(commands);
}
public void RecEngine_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
if(e.Result.Text == Atext.Text)
{
PressKey(0x1E);
}
else if (e.Result.Text == Dtext.Text)
{
PressKey(0x20);
}
}
我想更改“ command.Add(新字符串[] {Atext.Text,Dtext.Text)”中的值
我將命令列表放入文本框中,因此當我運行軟件時,當我想更改命令列表時,我只需要更改文本框內的單詞,然后單擊“保存”按鈕,命令列表就會改變。 問題是,當我單擊“保存”按鈕時,命令列表不會更改。
是否可以更改Choices對象和GrammarBuilder對象的值?
填充數組后,您不應修改該數組(可能是列表)。 我假設gBuilder
包含語音識別接受的所有命令。 如果是這樣,請添加所有將要使用的命令,並在RecEngine_SpeechRecognized
事件中檢查它們是否已啟用,然后才根據它們實際觸發操作。
給你一個基本的例子:
public void RecEngine_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
//mode could be a string or an enum variable
if(mode.Equals("LEFT_RIGHT") && e.Result.Text == Atext.Text)
{
PressKey(0x1E);
}
else if (mode.Equals("LEFT_RIGHT") && e.Result.Text == Dtext.Text)
{
PressKey(0x20);
}else if (mode.Equals("UP_DOWN") && e.Result.Text == Stext.Text)
{
PressKey(...);
}else if(mode.Equals("UP_DOWN") && e.Result.Text == Wtext.Text)
{
PressKey(....);
}
}
如果您的程序將變得更加復雜,那么更好的方法是創建一個已啟用命令的列表。 檢查該實現的示例:
public void RecEngine_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
//enabledCommands is a list of strings (or whatever can store Atext.Text and the others)
if(enabledCommands.Contains(e.Result.Text){ //First check if whatever command you received is enabled
if(e.Result.Text == Atext.Text) //then check what command it is and execute it
{
PressKey(0x1E);
}
}
}
如果您的程序要求您可以走得更遠並實現一個對象列表,則每個對象都包含一個命令和一個布爾值(指示是否已啟用該命令),但這比您需要的代碼要復雜得多。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.