繁体   English   中英

如何使用UI自动化选择树项?

[英]How to Select a tree item using UI Automation?

我正在尝试从应用程序中选择一个树项目,但得到“无法执行操作”。 我尝试使用UI Spy选择树项并得到相同的错误。

元素:“树项”“网络”名称:InvalidOperationException消息:无法执行操作。 堆栈跟踪:位于MS.Internal.Automation处MS.Internal.AutomationProxies.WindowsTreeView.TreeViewItem.System.Windows.Automation.Provider.ISelectionItemProvider.Select()位于System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode,IntPtr errorInfo) System.Windows.Automation.SelectionItemPattern.Select()上的.UiaCoreApi.CheckError(Int32 hr)

从UI Spy我知道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();

知道我在做什么错吗?

请以编程方式验证自动化元素“ Item”是否支持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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM