我有一个 powershell 表单,用于管理已安装的应用程序并提供一种快速卸载该应用程序的方法。我想做的是为表单上的按钮创建一个类构造函数,这样我就可以从另一个函数启用或禁用表单上按钮的状态。以下是我正在尝试做的事情,但我对其他建议持开放态度。
Add-Type -AssemblyName System.Windows.Forms
class App
{
[string] $Name
[bool] $Installed
[string] $Key32
[string] $Key64
[string] $Inst_Dir
[bool] $Current
[string] $Exec
[string] $User_Ver
[string] $Inst_Ver
App ([string] $Name, [bool] $Installed, [string] $Key32, [string] $Key64, [string] $Inst_Dir, [bool] $Current, [string] $Exec, [string] $User_Ver, [string] $Inst_Ver)
{
$this.Name = $Name
$this.Installed = $Installed
$this.Key32 = $Key32
$this.Key64 = $Key64
$this.Inst_Dir = $Inst_Dir
$this.current = $Current
$this.Exec = $Exec
$this.User_Ver = $User_Ver
$this.Inst_Ver = $Inst_Ver
}
}
class Button
{
[string] $Text
[int] $Width
[int] $Height
[string] $Font
[bool] $Enabled
Button ([string] $Text, [int] $Width, [int] $Height, [string] $Font, [bool] $Enabled)
{
$this.Text = $Text
$this.Width = $Width
$this.Height = $Height
$this.Font = $Font
$this.Enabled = $Enabled
}
}
#region Define and initialize Variables
$HKLM32 = ".\SOFTWARE\Key Location"
$HKLM64 = ".\SOFTWARE\Wow6432Node\Key Location"
# Initialize app classes
[App] $TestApp= [App]::new("TestApp", $false, "$HKLM32\TestApp $TestApp_Ver", "$HKLM64\TestApp $TestApp_Ver", "", $false, "", $TestApp_Ver, "")
$apps = $TestApp
Function create_form()
{
$Form = New-Object system.Windows.Forms.Form
$Form.Text = "Performance Test Auto Setup"
$Form.TopMost = $true
$Form.Width = 480
$Form.Height = 300
$Form.StartPosition = "CenterScreen"
$Form.FormBorderStyle = "Fixed3D"
$Form.MaximizeBox = $false
$Form.MinimizeBox = $false
[Button] $btnTestAppUninstall = [Button]::new("Uninstall", 95, 22, "Microsoft Sans Serif, 10", $true)
New-Object System.Drawing.Point(285, 9)
<#
$btnTestAppUninstall = New-Object system.windows.Forms.Button
$btnTestAppUninstall.Text = "Uninstall"
$btnTestAppUninstall.Width = 95
$btnTestAppUninstall.Height = 22
$btnTestAppUninstall.location = new-object system.drawing.point(285,9)
$btnTestAppUninstall.Font = "Microsoft Sans Serif,10"
$btnTestAppUninstall.Enabled = $false
$Form.controls.Add($btnTestAppUninstall)
$btnTestAppUninstall.Add_Click({uninstallApp($TestApp.Inst_Dir)})
#>
$btnTest = New-Object system.windows.Forms.Button
$btnTest.Text = "test"
$btnTest.Width = 95
$btnTest.Height = 22
$btnTest.location = new-object system.drawing.point(50,9)
$btnTest.Font = "Microsoft Sans Serif,10"
$Form.controls.Add($btnTest)
$btnTest.Add_Click({updateUI})
$Form.ShowDialog() | Out-Null
}
Function updateUI()
{
$apps | ForEach-Object {$app = [string]("$"+"btn$($_.Name)Uninstall")}
Write-Host $app.GetType() $btnTestAppUninstall.GetType()
}
create_Form
表单创建中间的注释掉部分是我最初在表单创建处创建按钮的方式,它工作正常。问题来自于在 $apps 数组上使用 foreach-object 循环的另一个函数中调整该按钮的 button.Enabled 声明。我已经将这个剪裁精简为表单上的一个按钮,启动时将显示一个测试按钮,但没有其他任何内容,因为类构造函数不会创建一个按钮。测试按钮功能“updateUI”是我尝试这样做的原因,因为在创建按钮变量名称时,我创建的是“字符串”而不是“system.drawings.button”对象,所以我无法访问启用的财产。
我希望我在这里尝试做的事情很清楚并且可以回答任何问题。我为在代码上草率的一半道歉,所以这是我开始处理类构造函数之前的片段,我有一个按钮但无法更改状态。
Add-Type -AssemblyName System.Windows.Forms
class App
{
[string] $Name
[bool] $Installed
[string] $Key32
[string] $Key64
[string] $Inst_Dir
[bool] $Current
[string] $Exec
[string] $User_Ver
[string] $Inst_Ver
App ([string] $Name, [bool] $Installed, [string] $Key32, [string] $Key64, [string] $Inst_Dir, [bool] $Current, [string] $Exec, [string] $User_Ver, [string] $Inst_Ver)
{
$this.Name = $Name
$this.Installed = $Installed
$this.Key32 = $Key32
$this.Key64 = $Key64
$this.Inst_Dir = $Inst_Dir
$this.current = $Current
$this.Exec = $Exec
$this.User_Ver = $User_Ver
$this.Inst_Ver = $Inst_Ver
}
}
#region Define and initialize Variables
$HKLM32 = ".\SOFTWARE\Key Location"
$HKLM64 = ".\SOFTWARE\Wow6432Node\Key Location"
# Initialize app classes
[App] $TestApp = [App]::new("TestApp", $false, "$HKLM32\TestApp $TestApp_Ver", "$HKLM64\TestApp $TestApp_Ver", "", $false, "", $TestApp_Ver, "")
$apps = $TestApp
Function create_form()
{
$Form = New-Object system.Windows.Forms.Form
$Form.Text = "Performance Test Auto Setup"
$Form.TopMost = $true
$Form.Width = 480
$Form.Height = 300
$Form.StartPosition = "CenterScreen"
$Form.FormBorderStyle = "Fixed3D"
$Form.MaximizeBox = $false
$Form.MinimizeBox = $false
$btnTestAppUninstall = New-Object system.windows.Forms.Button
$btnTestAppUninstall.Text = "Uninstall"
$btnTestAppUninstall.Width = 95
$btnTestAppUninstall.Height = 22
$btnTestAppUninstall.location = new-object system.drawing.point(285,9)
$btnTestAppUninstall.Font = "Microsoft Sans Serif,10"
$btnTestAppUninstall.Enabled = $false
$Form.controls.Add($btnTestAppUninstall)
$btnTestAppUninstall.Add_Click({uninstallApp($TestApp.Inst_Dir)})
$btnTest = New-Object system.windows.Forms.Button
$btnTest.Text = "test"
$btnTest.Width = 95
$btnTest.Height = 22
$btnTest.location = new-object system.drawing.point(50,9)
$btnTest.Font = "Microsoft Sans Serif,10"
$Form.controls.Add($btnTest)
$btnTest.Add_Click({updateUI})
$Form.ShowDialog() | Out-Null
}
Function updateUI()
{
$apps | ForEach-Object {$app = [string]("$"+"btn$($_.Name)Uninstall")}
Write-Host $app.GetType() $btnTestAppUninstall.GetType()
}
create_Form
还值得注意的是,我知道我可以使用直接调整状态
$btnTestAppUninstall.Enabled = $true
或者
$btnTestAppUninstalled.Enabled = $false
然而,这不是一个最佳解决方案,因为我试图解决这个问题的原因是因为表单上的每个按钮都必须在整个 updateUI 函数的多个位置有这两个选项,并且真实表单上有 22 个按钮。那里有很多 DRY 被破坏。如果我能完成这项工作,我可以简单地使用 foreach-object 循环遍历所有内容,并根据类对象中存储的值设置状态。
感谢您提供任何帮助,我已经尝试解决了几天。