与其使用可能会变得非常冗长且非常乏味的 Switch/Case 或 IF 布尔检查,我想知道是否可以寻找更好的方法来处理和处理命令。
例如:
if(settings.getName == Command)
{
Speak("I am here");
}
if("Get News Feed" == Command)
{
MyRSSFeed RSSNewsFeed = new MyRSSFeed();
RSSNewsFeed.GetFeed();
}
if 命令继续...这是我的 Switch 语句的片段:
switch (Command)
{
#region <-- Get Time Command -->
case "Time Please":
case "Whats the Time":
case "What Time is it":
GetCurrentTime();
break;
#endregion <-- Get Time Command -->
#region <-- Get Date Command -->
case "Whats the Date":
case "What Date is it":
case "Whats the Date Today":
case "What is the Date Today":
GetCurrentDate();
break;
#endregion <-- Get Date Command -->
#region <-- Media Player Commands -->
case "Play Bamboo Forest":
Data.MusicPlayer.Play(@"\Bamboo Forest Play List.wpl");
break;
case "Next Song":
Data.MusicPlayer.Next();
break;
case "Previous Song":
Data.MusicPlayer.Previous();
break;
case "Stop Music":
Data.MusicPlayer.Stop();
break;
case "Pause Music":
Data.MusicPlayer.Pause();
break;
case "Resume Music":
Data.MusicPlayer.Resume();
break;
case "Mute Music":
Data.MusicPlayer.Mute();
break;
case "Volume Up":
Data.MusicPlayer.VolumeUp();
break;
case "Volume Down":
Data.MusicPlayer.VolumeDown();
break;
#endregion <-- Media Player Commands -->
#region <-- Voice Recognition Control Commands -->
case "Stop Listening":
Audio.Listen.NewCommandRecognitionEngine.RecognizeAsyncCancel();
Audio.Voice.Speak("Ok");
Audio.Listen.Initialise(main);
break;
#endregion <-- Voice Recognition Control Commands -->
#region <-- Application Commands -->
case "Quiet":
Audio.Voice.Stop();
break;
case "Download":
Audio.Voice.Speak("Opening Download Window.");
main.dlInterface.ShowBitsJobs();
break;
case "Settings":
Audio.Voice.Speak("Opening Settings Window.");
main.settings.Show();
break;
case "Close":
if (main.dlInterface.Visable == true)
{
main.dlInterface.Hide();
Audio.Voice.Speak("Closing Download Window.");
}
if (main.settings.Visible == true)
{
main.settings.Hide();
Audio.Voice.Speak("Closing Settings Window.");
}
break;
case "Out of the way":
if (main.WindowState == System.Windows.Forms.FormWindowState.Normal)
{
main.WindowState = System.Windows.Forms.FormWindowState.Minimized;
Audio.Voice.Speak("My apologies");
}
break;
case "Where Are You":
if (main.WindowState == System.Windows.Forms.FormWindowState.Minimized)
{
main.WindowState = System.Windows.Forms.FormWindowState.Normal;
Audio.Voice.Speak("Here");
}
break;
default:
// Do Nothing here...
break;
}
我有一个包含命令的 SQL 数据库。我根据需要将命令加载到其中。它有一个命令名称列和一个值列。我可以根据需要更改这些以添加更改或删除列。
目前,一旦一个命令被识别,我使用 IF 语句和一个 Switch/Case Catch 的组合来捕捉识别的命令。
我曾考虑过以某种方式将 dll 放入文件夹中,以及如何在应用程序加载时进行扫描。如果我添加一个命令,然后以某种方式使用值字段来操作 dll 中的命令。
我意识到这是一个相当复杂的情况,但我觉得可以找到更好的解决方案来使这个过程更加简单。
请询问您是否需要更多信息。
[编辑] Paqogomez 已经回答了这个问题。请参阅下面的工作示例:
using System;
using System.Linq;
using MyApp.AppCommands;
using System.Reflection;
using System.Collections.Generic;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
MethodInfo myMethod;
var methods = new Commands();
myMethod = CommandFactory.GetCommandMethods("Time Please");
myMethod.Invoke(methods, null);
myMethod = CommandFactory.GetCommandMethods("Volume Down");
myMethod.Invoke(methods, null);
myMethod = CommandFactory.GetCommandMethods("Volume Up");
myMethod.Invoke(methods, null);
Console.ReadLine();
}
}
public static class CommandFactory
{
private static Dictionary<string, MethodInfo> commandMethods = new Dictionary<string, MethodInfo>();
public static MethodInfo GetCommandMethods(string Command)
{
MethodInfo methodInfo;
var myCommandMethods = new Commands();
if (commandMethods.Count == 0)
{
var methodNames = typeof(Commands).GetMethods(BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.Instance);
var speechAttributeMethods = methodNames.Where(y => y.GetCustomAttributes().OfType<CommandAttribute>().Any());
foreach (var speechAttributeMethod in speechAttributeMethods)
{
foreach (var attribute in speechAttributeMethod.GetCustomAttributes(true))
{
commandMethods.Add(((CommandAttribute)attribute).CommandValue, speechAttributeMethod);
}
}
methodInfo = commandMethods[Command];
}
else
{
methodInfo = commandMethods[Command];
}
return methodInfo;
}
}
}
namespace MyApp.AppCommands
{
public class Commands
{
[Command("Time Please")]
[Command("Whats the Time")]
[Command("What Time is it")]
public void GetTime()
{
Console.WriteLine(DateTime.Now.ToLocalTime());
}
[Command("Volume Down")]
public void VolumeDown()
{
Console.WriteLine("Volume Down 1");
}
[Command("Volume Up")]
public void VolumeUp()
{
Console.WriteLine("Volume Up 1");
}
}
[System.AttributeUsage(System.AttributeTargets.Method, AllowMultiple = true)]
public class CommandAttribute : System.Attribute
{
public string CommandValue { get; set; }
public CommandAttribute(string textValue)
{
this.CommandValue = textValue;
}
}
}
美丽的工作 Paqogomez,感谢您的分享!这是快速且非常优雅的!
就我而言,我需要调用的代码是:
private static void CommandRecognized(object sender, SpeechRecognizedEventArgs e)
{
MethodInfo myMethod;
var methods = new Commands();
myMethod = CommandFactory.GetCommandMethods(e.Result.Text);
myMethod.Invoke(methods, null);
}
这是语音识别引擎的事件处理程序:
CommandRecognitionEngine.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(CommandRecognized);