【问题标题】:Array method that takes userinput?接受用户输入的数组方法?
【发布时间】:2018-01-16 19:33:19
【问题描述】:

我目前正在处理我的一个项目,我称之为“自动语音检测器”基本上这个程序大部分时间都位于系统托盘中,只是监听用户输入。

我现在得出的结论是,我将无法用人们想要的所有命令填充“命令”数组,因此我决定要集成“AddCommand”用户输入。用户可以自己输入所需的命令,然后程序将执行我决定执行的任何操作。但是我真的需要帮助。

如何创建一个带有 1 个参数的字符串数组方法,该参数将是用户输入字符串“命令”。将该用户输入添加到字符串数组。这可能吗? 这是我设置的“默认”命令的当前给定代码。

            Choices commands = new Choices();
            commands.Add(new string[] { "dollar", "euro", "hotmail", "notepad", "outlook", "onedrive", "discord" });
            GrammarBuilder gBuilder = new GrammarBuilder();
            gBuilder.Append(commands);
            Grammar grammar = new Grammar(gBuilder);

所以它会像这样工作,只是像 commands2 这样的其他数组能够接受 1 个参数并将其插入到数组中。如果需要,下面的代码是整个项目。

public partial class Form1 : Form
{
    public SpeechRecognitionEngine recEngine; 
    public static bool keyHold = false;

    NotifyIcon IconPicture;
    Icon ActiveIcon;

    public Form1()
    {
        InitializeComponent();

    }

    private void Form1_Load(object sender, EventArgs e)
    {
        #region Icon and windows system tray dropdown text & click events
        //Creating icon and setting it to default.
        ActiveIcon = new Icon("speak_lzW_icon.ico");
        IconPicture = new NotifyIcon();
        IconPicture.Icon = ActiveIcon;
        //iconPicture.Visible = true;

        //Creating menu item for window in system tray.
        //MenuItem ProgNameMenuItem = new MenuItem("Voice detection by: Lmannen");
        MenuItem QuitMenuItem = new MenuItem("Quit");           
        ContextMenu contextMenu = new ContextMenu();
        contextMenu.MenuItems.Add(ProgNameMenuItem);
        contextMenu.MenuItems.Add(QuitMenuItem);

        //Adding the icon to the system tray window.
        IconPicture.ContextMenu = contextMenu;

        //System tray click event handlers
        QuitMenuItem.Click += QuitMenuItem_Click;
        IconPicture.MouseDoubleClick += IconPicture_MouseDoubleClick1;
        #endregion

        #region SpeechRecognition commands & event handlers
        recEngine = new SpeechRecognitionEngine();
        recEngine.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(recEngine_SpeechRecognized);
        recEngine.AudioStateChanged += new EventHandler<AudioStateChangedEventArgs>(recEngine_AudioStateChange);

        Choices commands = new Choices();
        commands.Add(new string[] { "dollar", "euro", "hotmail", "notepad", "outlook", "onedrive", "discord" });
        GrammarBuilder gBuilder = new GrammarBuilder();
        gBuilder.Append(commands);
        Grammar grammar = new Grammar(gBuilder);

        recEngine.SetInputToDefaultAudioDevice();
        recEngine.LoadGrammarAsync(grammar);
        recEngine.RequestRecognizerUpdate();
        recEngine.RecognizeAsync(RecognizeMode.Multiple);
        #endregion          
    }

    internal void recEngine_AudioStateChange(object sender, AudioStateChangedEventArgs e)
    {
        InputStatusLbl.Text = string.Format("{0}", e.AudioState);
    }

    internal static void recEngine_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
    {
        switch(e.Result.Text)
        {
            case "notepad":
                System.Diagnostics.Process.Start("notepad.exe");
                break;

            case "hotmail":
                System.Diagnostics.Process.Start("https://outlook.live.com/owa/");
                break;

            case "outlook":
                System.Diagnostics.Process.Start("https://outlook.live.com/owa/");
                break;

            case "ondrive":
                System.Diagnostics.Process.Start("https://onedrive.live.com/");
                break;

            case "discord":
                string name = Environment.UserName;
                string path = string.Format(@"C:\Users\{0}\AppData\Local\Discord\app-0.0.300\Discord.exe", name);
                System.Diagnostics.Process.Start(path);
                break;
        }
    }

    private void Form1_Resize(object sender, EventArgs e)
    {
        if(WindowState == FormWindowState.Minimized)
        {
            ShowInTaskbar = false;
            ShowIcon = false;
            IconPicture.Visible = true;

        }
    }

    private void IconPicture_MouseDoubleClick1(object sender, MouseEventArgs e)
    {
        ShowInTaskbar = true;
        IconPicture.Visible = false;
        ShowIcon = true;
        WindowState = FormWindowState.Normal;
    }

    private void QuitMenuItem_Click(object sender, EventArgs e)
    {
        IconPicture.Dispose();
        this.Close();
    }



    private void addToolStripMenuItem_Click(object sender, EventArgs e)
    {
        string input = Microsoft.VisualBasic.Interaction.InputBox("Add a voice-command by text", "Command");
        MessageBox.Show(input + " is now added to the command list");
    }
}

}

【问题讨论】:

  • 您在寻找List<T>吗?你的问题很混乱
  • 我不太清楚你卡在哪里了。你可以创建一个方法来接受你想要的任何参数。您已经知道如何将string[] 数组“添加”到Choices 对象。我想我只是看不到这里的实际问题/问题。您是否尝试过某些特定方式不起作用的方法?
  • 嘿,你现在有我自己的“选择命令”对象集。现在我想要另一个命令对象“Choices command2”代替它可以接受用户输入,而不是我添加我能想出的每一个可能的命令。基本上通过“addtoolstripmenuitem_Click 事件”,我希望用户能够接受字符串输入并将其插入“Choices command2”。基本上我有 1 个数组用于我自己设置的默认命令,然后我有第二个数组,用户可以在其中输入他们的 assired 命令。

标签: c# arrays


【解决方案1】:

对你的任务有一些背景,我相信你需要一本字典。它将是表单级别的公共变量。键是命令,值是执行路径。在表单中,您将在分配事件之前使用 5 个值对其进行初始化。

Public Dictionary<String, String> Commands = new Dictionary<String, String>();

所以在表单加载中(你需要其中的 5 个):

Dictionary.Add("notepad", "notepad.exe");
Dictionary.Add("hotmail", "https://outlook.live.com/owa/");

您将搜索字典而不是 case 语句,如果键存在,您将启动值。假设您有一个名为 commands 的字典:

string command = "";
if ( Commands.TryGetValue(e.Result.Text, out command))
    System.Diagnostics.Process.Start(command)

add命令将路径中的命令名和应用程序路径添加到字典中。

Commands.Add(commandName, pathToCommand);

请注意,当您执行此操作时,您还应该保存到用户本地应用程序数据区域中的一个文件,该文件可以在表单加载时恢复,因此它被保留,但这超出了范围。

【讨论】:

  • 嘿,到目前为止,感谢您的帮助 :),我清楚地了解您的意图,我认为这是一个好主意,但我只是不知道将什么声明为公共变量。至于我认为我可以管理的其余代码!
  • 命令列表 - 公共词典 Commands = new Dictionary();
  • 我确实喜欢这个。公共部分类 Form1 : Form { public static Dictionary CommandsList { get;放; } }。然后在 form1_load 下我添加了这个“ CommandsList = new Dictionary(string, string>(); 。只要我来了。我希望这是正确的,但我不知道我将如何在 GrammarBuilder 中处理这本字典?这里是完整的代码,所以你可以看到它的样子。pastebin.com/tXPPCK8W
  • 我还需要使用这个吗?选择命令 = new Choices();命令。添加(命令); GrammarBuilder gBuilder = new GrammarBuilder(); gBuilder.Append(命令);语法语法 = new Grammar(gBuilder);
  • 是的......因为当你添加一个命令时,它也需要进入你的语法
猜你喜欢
  • 2015-01-08
  • 1970-01-01
  • 1970-01-01
  • 2014-05-04
  • 2020-11-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多