使用System.WindowsAutomation库

时间:2018-12-15 19:59:22

标签: winforms automation ui-automation

我正在尝试使用System.Windows.Automation使WinForms应用程序自动化,我能够检索元素(选项卡项)

var tab = window.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Tab));

,但无法获得选择它的模式(SelectionItemPattern)。这是该元素的模式列表及其可用性(即无):

Patterns for ControlType.TabItem: Is DockPatternIdentifiers.Pattern available: no Is ExpandCollapsePatternIdentifiers.Pattern available: no Is GridItemPatternIdentifiers.Pattern available: no Is GridPatternIdentifiers.Pattern available: no Is InvokePatternIdentifiers.Pattern available: no Is ItemContainerPatternIdentifiers.Pattern available: no Is MultipleViewPatternIdentifiers.Pattern available: no Is RangeValuePatternIdentifiers.Pattern available: no Is ScrollItemPatternIdentifiers.Pattern available: no Is ScrollPatternIdentifiers.Pattern available: no Is SelectionItemPatternIdentifiers.Pattern available: no <------------ Is SelectionPatternIdentifiers.Pattern available: no Is SynchronizedInputPatternIdentifiers.Pattern available: no Is TableItemPatternIdentifiers.Pattern available: no Is TablePatternIdentifiers.Pattern available: no Is TextPatternIdentifiers.Pattern available: no Is TogglePatternIdentifiers.Pattern available: no Is TransformPatternIdentifiers.Pattern available: no Is ValuePatternIdentifiers.Pattern available: no Is VirtualizedItemPatternIdentifiers.Pattern available: no Is WindowPatternIdentifiers.Pattern available: no

当我使用inspect.exeVisualUIAVerifyNative.exe检查应用程序时,我可以看到模式在那里并且可用,从inspect.exe输出:

IsAnnotationPatternAvailable: false IsDragPatternAvailable: false IsDockPatternAvailable: false IsDropTargetPatternAvailable: false IsExpandCollapsePatternAvailable: false IsGridItemPatternAvailable: false IsGridPatternAvailable: false IsInvokePatternAvailable: false IsItemContainerPatternAvailable: false IsLegacyIAccessiblePatternAvailable: true IsMultipleViewPatternAvailable: false IsObjectModelPatternAvailable: false IsRangeValuePatternAvailable: false IsScrollItemPatternAvailable: false IsScrollPatternAvailable: false IsSelectionItemPatternAvailable: true <--------------------------------- IsSelectionPatternAvailable: false IsSpreadsheetItemPatternAvailable: false IsSpreadsheetPatternAvailable: false IsStylesPatternAvailable: false IsSynchronizedInputPatternAvailable: false IsTableItemPatternAvailable: false IsTablePatternAvailable: false IsTextChildPatternAvailable: false IsTextEditPatternAvailable: false IsTextPatternAvailable: false IsTextPattern2Available: false IsTogglePatternAvailable: false IsTransformPatternAvailable: false IsTransform2PatternAvailable: false IsValuePatternAvailable: false IsVirtualizedItemPatternAvailable: false IsWindowPatternAvailable: false

对于使用纯WinForms控件构建的小型应用程序,它工作正常。在这种情况下,该应用是使用第三方组件构建的。我的结论是,在这些控件中实现自动化的方式存在问题。

该模式真的可用吗? inspect.exe是否通过其他API获得使用?

我可以在VisualUIAVerifyNative中运行可访问性测试(即Pattern Tests> Priority 3 Tests> Select.1.4测试)以选择选项卡。或者,我可以通过在属性网格中执行命令来选择(Patterns> SelectionItemPattern> Select)。

好像我的C#代码中缺少重要的内容。

有人可以帮忙吗?谢谢!

1 个答案:

答案 0 :(得分:0)

我找到了解决方案-我现在不是引用System.Windows.Automation.dll,而是引用Interop.UIAutomationClient.dll。该DLL是UIAutomationVerify分发的一部分,或者可以使用TlbImp.exe生成(请参见How to build a interop dll from the UIAutomationClient.dll)。