PowerShell中的Windows Forms事件 - 发送器和PowerShell中的EventArgs

5

如何在PowerShell中正确处理Windows表单控件的事件并使用SenderEventArgs

以下C#代码在PowerShell中的等效代码是什么?

button.MouseClick += (sender, e) => {
    MessageBox.Show($"{((Control)sender).Name} \n {e.Location}");
};
1个回答

11

若要正确处理 PowerShell 中 Windows Forms 控件的事件并利用 SenderEventArgs,您可以使用以下任一选项:

  • 为脚本块定义 sendere 参数
  • 使用 $this$_ 变量

为脚本块定义 sendere 参数

与 C# 中的 Lambda 事件处理程序一样,您可以为脚本块定义 param($sender,$e)

$button.Add_MouseClick({param($sender,$e)
    [System.Windows.Forms.MessageBox]::Show(" $($sender.Name) `n $($e.Location)")
})

使用$this$_变量

$this是事件的发送者,$_是事件参数:

$button.Add_MouseClick({
    [System.Windows.Forms.MessageBox]::Show(" $($this.Name) `n $($_.Location)")
})

有没有可能添加另一个参数 param($sender, $e, $is='MouseClick') - Alban

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接