【问题标题】:UIA: Get ControlType from control type name (string)UIA:从控件类型名称(字符串)中获取 ControlType
【发布时间】:2012-12-19 21:21:53
【问题描述】:

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

【问题讨论】:

    标签: ui-automation microsoft-ui-automation uia


    【解决方案1】:

    我发现这样的方式,使用 PresentationCore.dll,对我来说很奇怪,这样的枚举在标准 UIA DLL 中不存在。另外请注意,ControlType 类中有一个错误,我猜是由于它的私有 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);
    

    【讨论】:

    • 关于 ControlType 类中的错误的好建议!
    猜你喜欢
    • 2014-10-18
    • 2011-08-04
    • 1970-01-01
    • 1970-01-01
    • 2011-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多