1

我正在尝试通过 Python.NET(https://github.com/FlaUI/FlaUI/blob/master/src/FlaUI.Core/Tools/Retry.csFlaUI.Core.Tools.Retry )调用 FlaUI类方法。但是,我无法弄清楚如何使用.WhileNullSystem.Func

该方法具有以下签名,

public static RetryResult<T> WhileNull<T>(Func<T> checkMethod, TimeSpan? timeout = null, TimeSpan? interval = null, bool throwOnTimeout = false, bool ignoreException = false, string timeoutMessage = null)

这是我想在 Python.NET 中复制的工作 C# 代码,

using FlaUI.Core;
using FlaUI.Core.Conditions;
using FlaUI.Core.Tools;
using FlaUI.Core.Definitions;
using FlaUI.Core.AutomationElements;
using FlaUI.UIA3;

var app = Application.Launch("software.exe");

var mainWindow = app.GetMainWindow(new UIA3Automation());

ConditionFactory cf = new ConditionFactory(new UIA3PropertyLibrary());
Retry.WhileNull(() => mainWindow.FindFirstDescendant(
    cf => cf.ByName("Connect")), throwOnTimeout: true);

我在 Python.NET 中创建了以下代码(应用程序启动工作,我能够获得主窗口),

import clr
import sys
flaui_core_path = 'C:\\path\\to\\flaui.core\\dll'
flaui_uia3_path = 'C:\\path\\to\\flaui.uia3\\dll'
interop_uiautomation_path = 'C:\\path\\to\\interop.uiautomationclient\\dll'
sys.path.append(flaui_core_path)
clr.AddReference('FlaUI.Core')
sys.path.append(interop_uiautomation_path)
clr.AddReference('Interop.UIAutomationClient')
sys.path.append(flaui_uia3_path)
clr.AddReference('FlaUI.UIA3')
from FlaUI.Core import Application
from FlaUI.Core.AutomationElements import AutomationElement
from FlaUI.Core.Conditions import ConditionFactory, ConditionBase
from FlaUI.Core.Tools import Retry
from FlaUI.UIA3 import UIA3Automation
from FlaUI.UIA3 import UIA3PropertyLibrary
clr.AddReference('System')
import System

app = Application.Launch('software.exe')
main_window = app.GetMainWindow(UIA3Automation())
cf = ConditionFactory(UIA3PropertyLibrary())
def find_first_descendant():
    def cf_by_name(cf):
        return cf.ByName("Connect")
    return main_window.FindFirstDescendant(
        System.Func[ConditionFactory, ConditionBase](cf_by_name)
    )
Retry.WhileNull(System.Func[AutomationElement](find_first_descendant), throwOnTimeout=True)

上面的代码启动软件。但是,进入后会抛出以下错误Retry.WhileNull

Traceback (most recent call last):
  File ".\sample.py", line 30, in <module>
    Retry.WhileNull(System.Func[AutomationElement](find_first_descendant), throwOnTimeout=True)
TypeError: No method matches given arguments for WhileNull: (<class 'System.0, Culture=neutral, PublicKeyToken=null]]'>) 
4

1 回答 1

1

显式提供函数泛型类型参数将起作用:

WhileNull[System.Int32](
  System.Func[System.Int32](my_func),
  throwOnTimeout=True)

目前pythonnet只能在直接使用泛型时解析它们(例如,如果第一个参数是T,而不是Func<T>)。

于 2022-02-04T19:26:53.997 回答