返回列表 发帖

[问题求助] C#代码中lambda 表达式转成powershell代码

本帖最后由 小白龙 于 2025-3-21 11:40 编辑

我想把下面这行C#代码转为powershell代码, 里面有个lambda 表达式,即cf => cf.ByAutomationId("CommandButton_7")
所有ai把我转晕了, 也没搞定, 求路过大佬支招, 多谢

https://github.com/FlaUI/FlaUI/b ... ilityMethods.cs#L79

原C#代码
dontSaveButton = modalWindows[0].FindFirstDescendant(cf => cf.ByAutomationId("CommandButton_7")).AsButton();COPY
所有ai都认为下面这样改, 但是实际使用报错
$dontSaveButton = $modalWindows[0].FindFirstDescendant([FlaUI.Core.Definitions.Condition]::ByAutomationId("CommandButton_7")).AsButton()COPY
powershell一向是简化c#代码, 在这里却不行了, 即便写通了, 估计也得一堆代码吧

应该是把相关的代码写在花括号中, 类似下面这样, 但是还是报错
$dontSaveButton = $modalWindows[0].FindFirstDescendant( { param($cf); $cf.ByAutomationId("CommandButton_7") } )COPY

C#下面都可以执行, 但是改成powershell死活就是不行
var conditionFactory = window.Automation.ConditionFactory;
var condition = conditionFactory.ByAutomationId("CommandButton_7");
dontSaveButton = modalWindows[0].FindFirstDescendant(condition).AsButton();COPY

TOP

试试将块转成action或func委托 ,例如
[func[xxx,yyy]]{$args[0].ByAutomationId("CommandButton_7")}COPY
xxx换成参数类型
yyy换成返回类型

TOP

回复 3# Five66


    怎么都不行, 真是怪了

TOP

回复 1# 小白龙

param语句后不应当有分号吧
你好

TOP

回复 5# jyswjjgdwtdtj


    试了也不行

TOP

回复 3# Five66


     发生错误: 方法调用失败,因为 [System.Char] 不包含名为“FindFirstDescendant”的方法。

TOP

回复 7# 小白龙


$modalWindows[0]的问题
就如报错说明的那样[System.Char] 不包含名为“FindFirstDescendant”的方法。也就是 $modalWindows[0] 类型为 [System.Char] ,没有叫做  FindFirstDescendant 的方法
[FlaUI.Core.AutomationElements.AutomationElement] 类型才有名为 FindFirstDescendant 的方法 ,请确保 $modalWindows[0] 的类型为 [FlaUI.Core.AutomationElements.AutomationElement]

TOP

本帖最后由 小白龙 于 2025-3-22 05:46 编辑

回复 8# Five66

多谢大佬指导, 确实是哪个问题,
我是想把下面链接的c#函数改为powershell
https://github.com/FlaUI/FlaUI/b ... ilityMethods.cs#L67
现在的问题是, 下面这行C#代码怎样正确改为powershell, 得先把上游问题解决掉
var modalWindows = Retry.WhileEmpty(() => window.ModalWindows, TimeSpan.FromSeconds(1)).Result;COPY
下面代码是gpt生成的powershell, 改的不正确
function CloseWindowWithDontSave {
    param($window)
    $window.Close()
    [Wait]::UntilInputIsProcessed()
    # 获取窗口的模态对话框
    $modalWindows = [Retry]::WhileEmpty({$window.ModalWindows}, [TimeSpan]::FromSeconds(5)).Result
    # 查找 "不保存" 按钮
    if ([Tools.OperatingSystem]::IsWindows11()) {
        $dontSaveButton = $modalWindows[0].FindFirstDescendant("SecondaryButton").AsButton()
    } else {
        $dontSaveButton = $modalWindows[0].FindFirstDescendant( { param($cf) $cf.ByAutomationId("CommandButton_7")} ).AsButton()
    }
    # 点击 "不保存" 按钮
    $dontSaveButton.Invoke()
}COPY
原C#代码如下:
        public static void CloseWindowWithDontSave(Window window)
        {
            window.Close();
            Wait.UntilInputIsProcessed();
            var modalWindows = Retry.WhileEmpty(() => window.ModalWindows, TimeSpan.FromSeconds(1)).Result;
            Button dontSaveButton;
            if (Tools.OperatingSystem.IsWindows11())
            {
                dontSaveButton = modalWindows[0].FindFirstDescendant("SecondaryButton").AsButton();
            }
            else
            {
                dontSaveButton = modalWindows[0].FindFirstDescendant(cf => cf.ByAutomationId("CommandButton_7")).AsButton();
            }
            dontSaveButton.Invoke();
        }COPY

TOP

回复 9# 小白龙


试试
function CloseWindowWithDontSave {
    param($window)
    $window.Close()
    [FlaUI.Core.Input.Wait]::UntilInputIsProcessed()
    # 获取窗口的模态对话框
    $modalWindows = [FlaUI.Core.Tools.Retry]::WhileEmpty([func[[FlaUI.Core.AutomationElements.Window[]]]]{$window.ModalWindows}, [TimeSpan]::FromSeconds(1)).Result
    # 查找 "不保存" 按钮
    if ([FlaUI.Core.Tools.OperatingSystem]::IsWindows11()) {
        $dontSaveButton = $modalWindows[0].FindFirstDescendant("SecondaryButton").AsButton()
    } else {
        $dontSaveButton = $modalWindows[0].FindFirstDescendant( { param($cf) $cf.ByAutomationId("CommandButton_7")} ).AsButton()
    }
    # 点击 "不保存" 按钮
    $dontSaveButton.Invoke()
}COPY

TOP

回复 10# Five66


    不行报错

C:\Users\Administrator\Desktop\FLAUI\_MouseTests_Click.ps1 : 发生错误: 找不到“WhileEmpty”的重载,参数计数为:“2”。
    + CategoryInfo          : NotSpecified: ( [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,_MouseTests_Click.ps1

TOP

本帖最后由 小白龙 于 2025-3-23 01:04 编辑

回复 10# Five66


    大佬, 我在把下面链接中的C#函数改为powershell, 上面要转的代码就是, 试两天了, 都没结果
https://github.com/FlaUI/FlaUI/b ... s/MouseTests.cs#L27

TOP

回复 11# 小白龙


    试试出错那行换成 ,不行的话估计得调用泛型版本的WhileEmpty方法 ,ps7.3之前调用net泛型方法挺麻烦的 ,自己网上搜索 ,或者干脆试试直接换成 $modalWindows=$window.ModalWindows
$modalWindows = [FlaUI.Core.Tools.Retry]::WhileEmpty([func[[FlaUI.Core.AutomationElements.Window[]]]]{$window.ModalWindows}, [TimeSpan]::FromSeconds(1),$null,$false,$false,$null).ResultCOPY

TOP

回复 13# Five66


    还是不行
C:\Users\Administrator\Desktop\FLAUI\_MouseTests_Click.ps1 : 发生错误: 方法调用失败,因为 [FlaUI.Core.AutomationElements.AutomationElement] 不包含名为“AsButton”的
方法。
    + CategoryInfo          : NotSpecified: ( [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,_MouseTests_Click.ps1

TOP

回复 14# 小白龙


    将
$dontSaveButton = $modalWindows[0].FindFirstDescendant("SecondaryButton").AsButton()COPY
$dontSaveButton = $modalWindows[0].FindFirstDescendant( { param($cf) $cf.ByAutomationId("CommandButton_7")} ).AsButton()COPY
换成
$dontSaveButton=[FlaUI.Core.AutomationElements.AutomationElementExtensions]::AsButton($modalWindows[0].FindFirstDescendant("SecondaryButton"))COPY
$dontSaveButton=[FlaUI.Core.AutomationElements.AutomationElementExtensions]::AsButton($modalWindows[0].FindFirstDescendant( { param($cf) $cf.ByAutomationId("CommandButton_7")} ))COPY

TOP

返回列表