如何从自定义控件获取名称/值,如Microsoft的Inspect工具?

时间:2015-09-28 10:04:04

标签: microsoft-ui-automation

有人知道如何获取自定义控件类型的AutomationElement的名称/值吗?请参阅下面的链接图像。我有兴趣获取Microsoft提供的Inspect工具突出显示的AutomationElement的名称/值。我试过检查AutomationElement是否支持IAccessible,因为IsLegacyIAccessiblePatternAvailableProperty是真的,从Inspect工具开始。我没有太多运气与下面的代码片段,我得到的只是表格Control的AutomationElement的所有子项的空字符串(图像中突出显示的AutomationElement的父代)。请指教。

AutomationElement sibling = TreeWalker.RawViewWalker.GetFirstChild(table);

            while (sibling != null)
            {

                if ((Boolean)sibling.GetCurrentPropertyValue(AutomationElementIdentifiers.IsLegacyIAccessiblePatternAvailableProperty))
                {
                    var pattern = ((LegacyIAccessiblePattern)sibling.GetCurrentPattern(LegacyIAccessiblePattern.Pattern));
                    var name = pattern.GetIAccessible().accValue;

                    //allChildren.Add(sibling);
                    if (((string)name).IndexOf(deviceId) != -1)
                    {
                        AutomationElement device = TreeWalker.RawViewWalker.GetPreviousSibling(sibling);

                        if (device != null)
                        {
                            checkBox = TreeWalker.RawViewWalker.GetPreviousSibling(device);
                            break;
                        }
                    }
                }

                sibling = TreeWalker.RawViewWalker.GetNextSibling(sibling);
            }

http://i.stack.imgur.com/78I8g.png

1 个答案:

答案 0 :(得分:0)

要获取 NameProperty ,请使用此选项:

  

object temp = yourElement.GetCurrentPropertyValue(AutomationElement.NameProperty);   temp.ToString();

注意:yourElement是一个AutomationElement。例如。表中的IPad的AutomationElement

要获取某些AutomationElement的值,您需要实现ValuePattern。我在你的截图中看不到。 (如果我错了,请纠正我)

btw:提供的屏幕截图没有显示NameProperty也没有显示值 - 我想刷新Inspect会做到这一点(摆脱许多[不支持]。如果Inspect失去对窗口的跟踪,这通常会发生(例如窗户关闭)

btw2:从我的角度来看AutomationElement.FindFirst&与TreeWalker相比,AutomationElement.FindAll是一个非常方便的API - 仅在少数情况下我更喜欢TreeWalker而不是FindFirst / All

相关问题