我正在尝试通过 Python.NET 使用来自 FlaUI 接口的通用成员。
下面的 C# 代码有效,
// myLegacyTreeItem is a valid AutomationElement
// this works and the default action expands the tree node
myLegacyTreeItem.Patterns.LegacyIAccessible.Pattern.DoDefaultAction();
但是,下面相应的 Python.NET 代码不起作用,
# my_legacy_tree_item is a valid element
my_legacy_tree_item.Patterns.LegacyIAccessible.Pattern.DoDefaultAction()
我收到以下错误,
AttributeError: 'UIA3FrameworkAutomationElement' object has no attribute 'LegacyIAccessible'
我可以看到该LegacyIAccessible
成员正在使用通用语法。但我无法弄清楚如何将它绑定到具体类型。
https://github.com/FlaUI/FlaUI/blob/master/src/FlaUI.Core/FrameworkAutomationElementBase.Patterns.cs
using FlaUI.Core.Patterns;
namespace FlaUI.Core
{
public abstract partial class FrameworkAutomationElementBase : FrameworkAutomationElementBase.IFrameworkPatterns
{
private IAutomationPattern<IAnnotationPattern> _annotationPattern;
private IAutomationPattern<IDockPattern> _dockPattern;
private IAutomationPattern<IDragPattern> _dragPattern;
private IAutomationPattern<IDropTargetPattern> _dropTargetPattern;
private IAutomationPattern<IExpandCollapsePattern> _expandCollapsePattern;
private IAutomationPattern<IGridItemPattern> _gridItemPattern;
private IAutomationPattern<IGridPattern> _gridPattern;
private IAutomationPattern<IInvokePattern> _invokePattern;
private IAutomationPattern<IItemContainerPattern> _itemContainerPattern;
private IAutomationPattern<ILegacyIAccessiblePattern> _legacyIAccessiblePattern;
...
public IFrameworkPatterns Patterns => this;
IAutomationPattern<IAnnotationPattern> IFrameworkPatterns.Annotation => _annotationPattern ?? (_annotationPattern = InitializeAnnotationPattern());
IAutomationPattern<IDockPattern> IFrameworkPatterns.Dock => _dockPattern ?? (_dockPattern = InitializeDockPattern());
IAutomationPattern<IDragPattern> IFrameworkPatterns.Drag => _dragPattern ?? (_dragPattern = InitializeDragPattern());
IAutomationPattern<IDropTargetPattern> IFrameworkPatterns.DropTarget => _dropTargetPattern ?? (_dropTargetPattern = InitializeDropTargetPattern());
IAutomationPattern<IExpandCollapsePattern> IFrameworkPatterns.ExpandCollapse => _expandCollapsePattern ?? (_expandCollapsePattern = InitializeExpandCollapsePattern());
IAutomationPattern<IGridItemPattern> IFrameworkPatterns.GridItem => _gridItemPattern ?? (_gridItemPattern = InitializeGridItemPattern());
IAutomationPattern<IGridPattern> IFrameworkPatterns.Grid => _gridPattern ?? (_gridPattern = InitializeGridPattern());
IAutomationPattern<IInvokePattern> IFrameworkPatterns.Invoke => _invokePattern ?? (_invokePattern = InitializeInvokePattern());
IAutomationPattern<IItemContainerPattern> IFrameworkPatterns.ItemContainer => _itemContainerPattern ?? (_itemContainerPattern = InitializeItemContainerPattern());
IAutomationPattern<ILegacyIAccessiblePattern> IFrameworkPatterns.LegacyIAccessible => _legacyIAccessiblePattern ?? (_legacyIAccessiblePattern = InitializeLegacyIAccessiblePattern());
...
}
}
如果我如下更改调用,则没有错误,但树项未展开,
((FrameworkAutomationElementBase.IFrameworkPatterns)(my_legacy_tree_item.Patterns)).LegacyIAccessible.Pattern.DoDefaultAction()
尝试使用下面的演员会抛出错误,
((FrameworkAutomationElementBase.IFrameworkPatterns[ILegacyIAccessiblePattern])(parent.Patterns)).LegacyIAccessible.Pattern.DoDefaultAction()
Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object.
at Python.Runtime.GenericUtil.GenericByName(String ns, String name, Int32 paramCount)
at Python.Runtime.ClassBase.type_subscript(IntPtr idx)