2013-05-10 34 views
2

我想從應用程序中選擇一個樹項目,但得到「操作無法執行」。我嘗試使用UI間諜選擇樹項目,並得到相同的錯誤。如何使用UI Automation選擇樹項目?

元素: 「項目」, 「網絡」 名稱:InvalidOperationException異常 消息:無法進行操作。 堆棧跟蹤:在MS.internal.AutomationProxies.WindowsTreeView.TreeViewItem.System.Windows.Automation.Provider.ISelectionItemProvider.Select() at System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode,IntPtr errorInfo) MS。 Internal.Automation.UiaCoreApi.CheckError在System.Windows.Automation.SelectionItemPattern.Select(的Int32小時) ()

從UI間諜我知道SelectionItem是支持的模式。這裏是一些代碼

AutomationElement Item = _ParentNode.FindFirst(TreeScope.Descendants, new AndCondition(
      new PropertyCondition(AutomationElement.NameProperty, "Network"), 
      new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.TreeItem))); 

SelectionItemPattern ItemToSelect = Item .GetCurrentPattern(SelectionItemPattern.Pattern) as SelectionItemPattern; 
ItemToSelect.Select(); 

任何想法我做錯了什麼?

+0

出現InvalidOperationException只是意味着它是不可能的,當時這個項目的 「某些原因」:http://msdn.microsoft.com/en-us/ library/system.windows.automation.selectionitempattern.select.aspx – 2013-05-11 06:12:35

+0

有什麼方法可以找出它不可能的原因並解決它嗎? – user2360570 2013-06-06 22:01:09

+0

一般來說,這是因爲操作不能由任何代碼或任何人執行。想想你作爲一個人,不能選擇一個樹項目的情況。例如,它可能出於焦點原因而發生。 – 2013-06-06 22:27:53

回答

0

請覈實編程如果自動化元件「項目」支持SelectionPattern控制模式與否。

private SelectionPattern GetSelectionPattern(
AutomationElement targetControl) 
{ 
SelectionPattern selectionPattern = null; 

try 
{ 
    selectionPattern = 
     targetControl.GetCurrentPattern(SelectionPattern.Pattern) 
     as SelectionPattern; 
} 
// Object doesn't support the SelectionPattern control pattern 
catch (InvalidOperationException) 
{ 
    return null; 
} 

return selectionPattern; 

}

來源:https://msdn.microsoft.com/en-us/library/ms604455(v=vs.110).aspx