1

我有一个从该方法中AutomationPattern检索到的对象列表。AutomationElement.GetSupportedPatterns()现在我需要知道那里有什么样的模式,我的意思是单项InvokePatternExpandCollapsePattern. 更具体地说,假设我们有一个AutomationPattern对象,并且我们知道ProgrammaticName该对象的属性是“InvokePatternIdentifiers.Pattern”字符串,我能以某种方式获得类型的对象InvokePattern吗?

我已经使用属性和一个大的 switch 语句实现了我自己解决这个问题的方法AutomationPattern.ProgrammaticName,还有其他方法可以做到这一点吗?

这是我的实现:

    public object GetActualAutomationPattern(AutomationElement element, AutomationPattern pattern)
    {
        switch (pattern.ProgrammaticName)
        {
            case "DockPatternIdentifiers.Pattern":
                return element.GetCurrentPattern(DockPattern.Pattern) as DockPattern;
            case "ExpandCollapsePatternIdentifiers.Pattern":
                return element.GetCurrentPattern(ExpandCollapsePattern.Pattern) as ExpandCollapsePattern;
            case "GridPatternIdentifiers.Pattern":
                return element.GetCurrentPattern(GridPattern.Pattern) as GridPattern;
            case "GridItemPatternIdentifiers.Pattern":
                return element.GetCurrentPattern(GridItemPattern.Pattern) as GridItemPattern;
            case "InvokePatternIdentifiers.Pattern":
                return element.GetCurrentPattern(InvokePattern.Pattern) as InvokePattern;
            case "MultipleViewPatternIdentifiers.Pattern":
                return element.GetCurrentPattern(MultipleViewPattern.Pattern) as MultipleViewPattern;
            case "RangeValuePatternIdentifiers.Pattern":
                return element.GetCurrentPattern(RangeValuePattern.Pattern) as RangeValuePattern;
            case "ScrollPatternIdentifiers.Pattern":
                return element.GetCurrentPattern(ScrollPattern.Pattern) as ScrollPattern;
            case "ScrollItemPatternIdentifiers.Pattern":
                return element.GetCurrentPattern(ScrollItemPattern.Pattern) as ScrollItemPattern;
            case "SelectionPatternIdentifiers.Pattern":
                return element.GetCurrentPattern(SelectionPattern.Pattern) as SelectionPattern;
            case "SelectionItemPatternIdentifiers.Pattern":
                return element.GetCurrentPattern(SelectionItemPattern.Pattern) as SelectionItemPattern;
            case "TablePatternIdentifiers.Pattern":
                return element.GetCurrentPattern(TablePattern.Pattern) as TablePattern;
            case "TableItemPatternIdentifiers.Pattern":
                return element.GetCurrentPattern(TableItemPattern.Pattern) as TableItemPattern;
            case "TogglePatternIdentifiers.Pattern":
                return element.GetCurrentPattern(TogglePattern.Pattern) as TogglePattern;
            case "TransformPatternIdentifiers.Pattern":
                return element.GetCurrentPattern(TransformPattern.Pattern) as TransformPattern;
            case "ValuePatternIdentifiers.Pattern":
                return element.GetCurrentPattern(ValuePattern.Pattern) as ValuePattern;
            case "WindowPatternIdentifiers.Pattern":
                return element.GetCurrentPattern(WindowPattern.Pattern) as WindowPattern;
            default:
                return pattern;
        }
    }
4

2 回答 2

3

您可以执行以下操作:

object patternObj = element.TryGetCurrentPattern(pattern);

然后只需测试类型是否是您想要的:

if (patternObj != null && patternObj is InvokePattern) {...} 
于 2012-07-28T01:14:40.243 回答
0

我希望这能帮到您。

/// <summary>  
/// Gets the toggle state of an element in the target application.  
/// </summary> 
/// <param name="element">The target element.</param>  

private bool IsElementToggledOn(AutomationElement element)
{
if (element == null)
{
    // TODO: Invalid parameter error handling. 
    return false;
}

Object objPattern;
TogglePattern togPattern;
if (true == element.TryGetCurrentPattern(TogglePattern.Pattern, out objPattern))
{
    togPattern = objPattern as TogglePattern;
    return togPattern.Current.ToggleState == ToggleState.On;
}
// TODO: Object doesn't support TogglePattern error handling. 
return false;
}  

我从这里拿走了这个

于 2014-04-24T09:33:31.670 回答