首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UIA:从控件类型名称(字符串)中获取ControlType

UIA:从控件类型名称(字符串)中获取ControlType
EN

Stack Overflow用户
提问于 2012-12-20 05:21:54
回答 1查看 1.9K关注 0票数 2

使用Microsoft UI自动化。我有一个代表UIA控件类型的字符串,比如"Window“或"Button”。我想获得一个适合这个字符串的ControlType对象。该怎么做呢?是否存在表示所有UIA控件类型的枚举?我发现只有ControlType有ControlType.LookupById(int)方法。但是我必须知道ID和名字之间的对应关系。当然,我可以使用所有可能的UIA控件类型创建自己的开关,甚至可以使用反射来获取ControlType工厂的所有成员。但我确定应该是更简单的方法..

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-12-20 19:27:24

我发现了这样一种方式,使用PresentationCore.dll,对我来说非常奇怪,这样的枚举在standart UIA DLL中不存在。另外请注意,ControlType类中有一个bug,我猜是由于它的私有静态构造函数造成的。如果你第一次调用ControlType.LookupById(enumId),它将返回null,但在第二次就可以了。解决方案非常简单--只需在使用前调用ToString,它将初始化静态构造函数:)

代码语言:javascript
复制
    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);
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13961400

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档