AutomationElement shows up using Inspect.exe but does show not up when using UIAutomationCore.dll or System.Windows.Automation

后端 未结 1 389
天命终不由人
天命终不由人 2020-12-23 17:09

TL;DR: What am I doing wrong that is causing the workspace pane to show up in Inspect Objects but not show up in my custom code?


I am trying t

相关标签:
1条回答
  • 2020-12-23 17:59

    Very nice question. Based upon the problem you've documented, it's clear that your conditional:

    PropertyCondition workspaceCond = new PropertyCondition(
     AutomationElement.NameProperty, "Workspace", PropertyConditionFlags.IgnoreCase);
    

    ... fails evaluation. Why?

    The answer is how your conditional is evaluated. Looking at your element tree, we notice this property for Workspace:

    IsWindowPatternAvailable:   false
    

    And for the main window and Untitled3:

    IsWindowPatternAvailable:   true
    

    From MSDN:

    UIA_IsWindowPatternAvailablePropertyId 30044
    

    Identifies the IsWindowPatternAvailable property, which indicates whether the Window control pattern is available for the automation element. If TRUE, a client can retrieve an IUIAutomationWindowPattern interface from the element.

    We find a repro in this thread, which implies the same failure pattern as the one you are currently experiencing. We also note the lack of Window properties present for this element because IUIAutomationWindowPattern is inaccessible.

    A workaround is available from the aforelinked thread. Instead of PropertyCondition, one might use:

    public class ConditionMatcher : IMatchConditions
    {
        public bool Matches(AutomationElement element, Condition condition)
        {
            return new TreeWalker(condition).Normalize(element) != null;
        }
    }
    

    Or, alternately, one might use the workaround you've given, provided your tree structure is guaranteed to be shallow (and thus, appropriate to the name of this site, will not trigger a stack overflow).

    Admittedly, this wasn't the most obvious issue. In the perfect world, MSDN should have better documentation on this topic.

    0 讨论(0)
提交回复
热议问题