Domanda

Using Microsoft UI Automation. I have a string which represents UIA control type, like "Window" or "Button". I would like to get a ControlType object which fits this string. How to do it? Is some enumerations exists which represents all the UIA control types? I found only that ControlType has ControlType.LookupById(int) method. But I have to know correspondence between ID and name. Of course, I can create my own switch with all the possible UIA control types, or even use reflection to get all the members of ControlType factory. But I'm sure should be easier way..

È stato utile?

Soluzione

I've found such way, using PresentationCore.dll, very strange for me, that such enum doesn't exist in standart UIA DLL. Also please pay attention, that there is a bug in ControlType class, I guess due to its private static constructor. If you call ControlType.LookupById(enumId) 1st time, it will return null, but in the 2nd time will be OK. The solution is quite simple -- just call ToString before usage, it will initialize the static constructor :)

    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);
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top