我有從AutomationElement.GetSupportedPatterns()
方法檢索AutomationPattern
對象的列表。現在我需要知道什麼樣的模式在那裏,我的意思是單項InvokePattern
或ExpandCollapsePattern
。更具體地講,讓我們假設我們有一個AutomationPattern
對象,我們知道這個對象的ProgrammaticName
屬性是「InvokePatternIdentifiers.Pattern」字符串,可不知何故,我得到InvokePattern
類型的對象?獲取實際InvokePattern對象從AutomationPattern對象的列表
我已經實現了我自己解決在使用AutomationPattern.ProgrammaticName
財產和一個大的switch語句這個問題的辦法,有另一種方式做到這一點?
這裏是我的implemetation:
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;
}
}
採取這個我不是你的目標是與此代碼是什麼完全清楚。爲什麼你就不能像做'對象patternObj = element.TryGetCurrentPattern(圖案);',然後就測試類型是否是你想要什麼:'如果(patternObj = NULL && patternObj是InvokePattern!){.. 。} – Chaser324
@ Chaser324謝謝,這正是我需要的,以某種方式從AutomationPattern對象中提取實際模式 – haynar