我有一个从AutomationPattern
方法检索到的AutomationElement.GetSupportedPatterns()
个对象的列表。现在我需要知道哪种模式,我的意思是单项InvokePattern
或ExpandCollapsePattern
。更具体地说,假设我们有一个AutomationPattern
对象,并且我们知道该对象的ProgrammaticName
属性是“InvokePatternIdentifiers.Pattern”字符串,我可以以某种方式获得类型为InvokePattern
的对象?
我已经使用AutomationPattern.ProgrammaticName
属性和一个大的switch语句实现了我自己解决这个问题的方法,还有另外一种方法吗?
这是我的实现:
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;
}
}
答案 0 :(得分:2)
您可以执行以下操作:
object patternObj = element.TryGetCurrentPattern(pattern);
然后只测试该类型是否符合您的要求:
if (patternObj != null && patternObj is InvokePattern) {...}
答案 1 :(得分: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;
}
我是从here
取得的