ui-automation - UIA:从控件类型名称(字符串)获取ControlType

标签 ui-automation microsoft-ui-automation uia

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

最佳答案

我发现这样的方式,使用PresentationCore.dll,对我来说很奇怪,这样的枚举在标准UIA DLL中不存在。另请注意,ControlType 类中存在一个错误,我猜是由于其私有(private) static 构造函数所致。如果你第一次调用 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);

关于ui-automation - UIA:从控件类型名称(字符串)获取ControlType,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13961400/

相关文章:

c# - 如何使用 Microsoft uiautomation 检查复选框?

c# - 如何使用 UI 自动化从 Edge 浏览器检索文本

accessibility - IAccessible、IAccessible2、UIAutomation 和 MSAA 之间有什么区别?

windows-7 - 有什么比 Sikuli 更好的工具用于 Windows 7(或最好是多平台)上的屏幕自动化

c# - AutomationElement.FindAll() 性能问题

xamarin.forms - UI自动化-如何区分正在显示的图像

ui-automation - UIA 验证无法识别 System.Windows.Forms.ListView 复选框

Selenium Webdriver等待来自另一个元素的元素

c# - 通过 AutomationElement 设置 DateTimePicker 元素