我不想使用开关/大小写或者布尔检查,这些检查会变得非常冗长和乏味,我想知道是否可以找到一种更好的方法来处理和处理命令。
例如:
if(settings.getName == Command)
{
Speak("I am here");
}
if("Get News Feed" == Command)
{
MyRSSFeed RSSNewsFeed = new MyRSSFeed();
RSSNewsFeed.GetFeed();
}如果命令继续..。下面是我的开关语句的一个片段:
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数据库。我根据需要将命令加载到其中。它有命令名Colum和值Colum。我可以根据需要更改这些列以添加、更改或删除列。
当前,一旦一个命令被识别,我将使用IF语句和开关/Case Catch组合来捕获已识别的命令。
我考虑过如何将dll放到文件夹中,以及如何扫描,然后加载应用程序。如果我添加了一个命令,然后以某种方式使用Value字段来执行dll中的命令。
我意识到这是一个相当复杂的情况,但我觉得可以找到一个更好的解决办法,使这一过程更加简单。
编辑:我已经看过了:http://social.msdn.microsoft.com/Forums/en-US/4f962dc0-aec2-4191-9fe2-e1dfeb1da5dd/voice-command-api
请询问您是否需要更多的信息。
编辑 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);发布于 2013-09-04 03:42:50
您正在寻找定义为这里的策略模式。
这将允许您非常容易地处理多个if语句。
工厂模式也可能适合你。工厂可以使用反射来确定要创建的命令。
您也可以将所有命令转储到字典中。
编辑:
考虑到最后一点代码示例,工厂就是您所需要的。我已经把一个放在下面了。
工厂简单地反映了MySpeechMethods中的所有方法,查找带有SpeechAttributes的方法,并将MethodInfo发回调用。如果需要从方法中返回值,可以将所有方法设置为返回相同类型(如字符串),或者查看泛型,但这将留给您。:)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using MyApp.SpeechMethods;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
var methods = new MySpeechMethods();
MethodInfo myMethod;
myMethod = SpeechFactory.GetSpeechMethod("Time Please");
myMethod.Invoke(methods, null);
myMethod = SpeechFactory.GetSpeechMethod("Volume Down");
myMethod.Invoke(methods, null);
myMethod = SpeechFactory.GetSpeechMethod("Volume Up");
myMethod.Invoke(methods, null);
}
}
public static class SpeechFactory
{
private static Dictionary<string, MethodInfo> speechMethods = new Dictionary<string, MethodInfo>();
public static MethodInfo GetSpeechMethod(string speechText)
{
MethodInfo methodInfo;
var mySpeechMethods = new MySpeechMethods();
if (speechMethods.Count == 0)
{
var methodNames =
typeof (MySpeechMethods).GetMethods(BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.Instance);
var speechAttributeMethods = methodNames.Where(y => y.GetCustomAttributes().OfType<SpeechAttribute>().Any());
foreach (var speechAttributeMethod in speechAttributeMethods)
{
foreach (var attribute in speechAttributeMethod.GetCustomAttributes(true))
{
speechMethods.Add(((SpeechAttribute)attribute).SpeechValue, speechAttributeMethod);
}
}
methodInfo = speechMethods[speechText];
}
else
{
methodInfo = speechMethods[speechText];
}
return methodInfo;
}
}
}
namespace MyApp.SpeechMethods
{
public class MySpeechMethods
{
[Speech("Time Please")]
[Speech("Whats the Time")]
[Speech("What Time is it")]
public void GetTime()
{
Console.WriteLine(DateTime.Now.ToLocalTime());
}
[Speech("Volume Down")]
public void VolumeDown()
{
Console.WriteLine("Volume Down 1");
}
[Speech("Volume Up")]
public void VolumeUp()
{
Console.WriteLine("Volume Up 1");
}
}
[System.AttributeUsage(System.AttributeTargets.Method, AllowMultiple = true)]
public class SpeechAttribute : System.Attribute
{
public string SpeechValue { get; set; }
public SpeechAttribute(string textValue)
{
this.SpeechValue = textValue;
}
}
}https://stackoverflow.com/questions/18604980
复制相似问题