我正在尝试从另一个SO帖子here实现解决方案,以便能够直接编写命令,而不是使用SRGMS XML文件作为命令输入。我相信这是帖子试图做的,但我在实现它时遇到了麻烦。
我的代码如下,但它不起作用(错误行旁边有注释)。如果有人能提供如何在WindowsForm应用程序中使用此代码的简单说明,我将非常感激。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Speech.Recognition;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Commands
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
LoadStart();
}
private void LoadStart()
{
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);
}
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;
}
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;
}
}
}
private void button1_Click(object sender, EventArgs e)
{
CommandRecognitionEngine.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(CommandRecognized);
}
private static void CommandRecognized(object sender, SpeechRecognizedEventArgs e)
{
MethodInfo myMethod;
var methods = new Commands();//ERROR HERE: Commands is a namespace but used as a type
myMethod = CommandFactory.GetCommandMethods(e.Result.Text);
myMethod.Invoke(methods, null);
}
}
}
答案 0 :(得分:1)
编译器无法区分您是指类还是名称空间Commands
,因为您有一个类和名称相同的命名空间。
执行以下任一操作
重构您的班级名称
重构您的命名空间名称
显示编译器,表示类Commands.Commands
答案 1 :(得分:0)
你有一个名为Commands的类,但它也在命名空间命令中,所以它不能区分它们。
更改命名空间或类名,或将该行更改为:
var methods = new Commands.Commands();