采用userinput的数组方法?

时间:2018-01-16 19:33:19

标签: c# arrays

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

我现在得出的结论是,我无法使用人们想要的所有命令填充“命令”数组,所以我决定要整合“AddCommand”用户输入。用户可以自己输入所需的命令,程序稍后会做我决定做的事情。但是我真的需要帮助。

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

            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);

所以它会像这样工作,只有像command2这样的其他数组能够获取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");
    }
}

}

1 个答案:

答案 0 :(得分:0)

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

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

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

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

您将搜索字典,而不是案例陈述,如果密钥存在,您将启动该值。假设你有一个叫做命令的词典,那就是:

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

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

Commands.Add(commandName, pathToCommand);

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