使用Microsoft UI自动化。我有一个代表UIA控件类型的字符串,比如"Window“或"Button”。我想获得一个适合这个字符串的ControlType对象。该怎么做呢?是否存在表示所有UIA控件类型的枚举?我发现只有ControlType有ControlType.LookupById(int)方法。但是我必须知道ID和名字之间的对应关系。当然,我可以使用所有可能的UIA控件类型创建自己的开关,甚至可以使用反射来获取ControlType工厂的所有成员。但我确定应该是更简单的方法..
发布于 2012-12-20 19:27:24
我发现了这样一种方式,使用PresentationCore.dll,对我来说非常奇怪,这样的枚举在standart UIA DLL中不存在。另外请注意,ControlType类中有一个bug,我猜是由于它的私有静态构造函数造成的。如果你第一次调用ControlType.LookupById(enumId),它将返回null,但在第二次就可以了。解决方案非常简单--只需在使用前调用ToString,它将初始化静态构造函数:)
using System.Windows.Automation.Peers;
// solving the bug with static constructor of ControlType..
ControlType.Button.ToString();
string controlTypeString = "Window";
AutomationControlType typeEnum;
bool result = Enum.TryParse(controlTypeString, true, out typeEnum);
if (result) typeEnum = (AutomationControlType)Enum.Parse(typeof(AutomationControlType), controlTypeString);
int enumId = (int)typeEnum + 50000;
ControlType controlType = ControlType.LookupById(enumId);https://stackoverflow.com/questions/13961400
复制相似问题