[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

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

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

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

TOP

试试将块转成action或func委托 ,例如
  1. [func[xxx,yyy]]{$args[0].ByAutomationId("CommandButton_7")}
复制代码
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, 得先把上游问题解决掉
  1. var modalWindows = Retry.WhileEmpty(() => window.ModalWindows, TimeSpan.FromSeconds(1)).Result;
复制代码
下面代码是gpt生成的powershell, 改的不正确
  1. function CloseWindowWithDontSave {
  2.     param($window)
  3.     $window.Close()
  4.     [Wait]::UntilInputIsProcessed()
  5.     # 获取窗口的模态对话框
  6.     $modalWindows = [Retry]::WhileEmpty({$window.ModalWindows}, [TimeSpan]::FromSeconds(5)).Result
  7.     # 查找 "不保存" 按钮
  8.     if ([Tools.OperatingSystem]::IsWindows11()) {
  9.         $dontSaveButton = $modalWindows[0].FindFirstDescendant("SecondaryButton").AsButton()
  10.     } else {
  11.         $dontSaveButton = $modalWindows[0].FindFirstDescendant( { param($cf) $cf.ByAutomationId("CommandButton_7")} ).AsButton()
  12.     }
  13.     # 点击 "不保存" 按钮
  14.     $dontSaveButton.Invoke()
  15. }
复制代码
原C#代码如下:
  1.         public static void CloseWindowWithDontSave(Window window)
  2.         {
  3.             window.Close();
  4.             Wait.UntilInputIsProcessed();
  5.             var modalWindows = Retry.WhileEmpty(() => window.ModalWindows, TimeSpan.FromSeconds(1)).Result;
  6.             Button dontSaveButton;
  7.             if (Tools.OperatingSystem.IsWindows11())
  8.             {
  9.                 dontSaveButton = modalWindows[0].FindFirstDescendant("SecondaryButton").AsButton();
  10.             }
  11.             else
  12.             {
  13.                 dontSaveButton = modalWindows[0].FindFirstDescendant(cf => cf.ByAutomationId("CommandButton_7")).AsButton();
  14.             }
  15.             dontSaveButton.Invoke();
  16.         }
复制代码

TOP

回复 9# 小白龙


试试
  1. function CloseWindowWithDontSave {
  2.     param($window)
  3.     $window.Close()
  4.     [FlaUI.Core.Input.Wait]::UntilInputIsProcessed()
  5.     # 获取窗口的模态对话框
  6.     $modalWindows = [FlaUI.Core.Tools.Retry]::WhileEmpty([func[[FlaUI.Core.AutomationElements.Window[]]]]{$window.ModalWindows}, [TimeSpan]::FromSeconds(1)).Result
  7.     # 查找 "不保存" 按钮
  8.     if ([FlaUI.Core.Tools.OperatingSystem]::IsWindows11()) {
  9.         $dontSaveButton = $modalWindows[0].FindFirstDescendant("SecondaryButton").AsButton()
  10.     } else {
  11.         $dontSaveButton = $modalWindows[0].FindFirstDescendant( { param($cf) $cf.ByAutomationId("CommandButton_7")} ).AsButton()
  12.     }
  13.     # 点击 "不保存" 按钮
  14.     $dontSaveButton.Invoke()
  15. }
复制代码

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
  1. $modalWindows = [FlaUI.Core.Tools.Retry]::WhileEmpty([func[[FlaUI.Core.AutomationElements.Window[]]]]{$window.ModalWindows}, [TimeSpan]::FromSeconds(1),$null,$false,$false,$null).Result
复制代码

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# 小白龙


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

TOP

返回列表