【问题标题】:How to Select a tree item using UI Automation?如何使用 UI 自动化选择树项?
【发布时间】:2013-05-10 22:02:53
【问题描述】:

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

元素:“树项”“网络” 名称:无效操作异常 消息:无法执行操作。 堆栈跟踪:在 MS.Internal.AutomationProxies.WindowsTreeView.TreeViewItem.System.Windows.Automation.Provider.ISelectionItemProvider.Select() 在 System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 错误代码,IntPtr 错误信息) 在 MS.Internal.Automation.UiaCoreApi.CheckError(Int32 小时) 在 System.Windows.Automation.SelectionItemPattern.Select()

从 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();

知道我做错了什么吗?

【问题讨论】:

  • InvalidOperationException 仅表示当时由于“某种原因”无法使用此项目:msdn.microsoft.com/en-us/library/…
  • 有没有办法找出它不可能的原因并解决它?
  • 一般来说,这是因为操作不能由任何代码或任何人执行。想想你作为人类无法选择树项的情况。例如,由于焦点原因,可能会发生这种情况。
  • 一个原因可能是该项目不在屏幕上
  • @IvanDanilov 那么如何将树项目滚动到视图中?滚动模式不适用于父树控件。

标签: c# .net visual-studio ui-automation


【解决方案1】:

以编程方式验证自动化元素“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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-25
    • 2018-04-19
    • 1970-01-01
    • 1970-01-01
    • 2023-03-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多