2013-05-10 150 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