2012-05-16 117 views
1

我有從AutomationElement.GetSupportedPatterns()方法檢索AutomationPattern對象的列表。現在我需要知道什麼樣的模式在那裏,我的意思是單項InvokePatternExpandCollapsePattern。更具體地講,讓我們假設我們有一個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; 
     } 
    } 
+1

採取這個我不是你的目標是與此代碼是什麼完全清楚。爲什麼你就不能像做'對象patternObj = element.TryGetCurrentPattern(圖案);',然後就測試類型是否是你想要什麼:'如果(patternObj = NULL && patternObj是InvokePattern!){.. 。} – Chaser324

+0

@ Chaser324謝謝,這正是我需要的,以某種方式從AutomationPattern對象中提取實際模式 – haynar

回答

2

你可以做類似:

object patternObj = element.TryGetCurrentPattern(pattern); 

,然後只是測試的類型是否是你想要什麼:

if (patternObj != null && patternObj is InvokePattern) {...} 
+0

只是正式添加這個作爲響應而不是評論。 – Chaser324

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