XAML 如何在代码执行期间禁用Powershell GUI按钮?

lb3vh1jj  于 2023-09-28  发布在  Shell
关注(0)|答案(1)|浏览(117)

我无法在功能操作期间禁用按钮。如果我在单击按钮时禁用它,从视觉上看,它看起来是禁用的,但它仍然工作(或者更确切地说,它排队单击)。
这是我正在测试的工作演示代码:

Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()

$Form                            = New-Object system.Windows.Forms.Form
$Form.ClientSize                 = '400,400'
$Form.text                       = "Form"
$Form.TopMost                    = $false
$Button1                         = New-Object system.Windows.Forms.Button
$Button1.text                    = "button"
$Button1.width                   = 100
$Button1.height                  = 25
$Button1.enabled                 = $true
$Button1.location                = New-Object System.Drawing.Point(214,59)
$Button1.Font                    = 'Microsoft Sans Serif,10'

$Form.controls.AddRange(@($Button1))

Function Test {
    Write-Host "Clicked"
    $Button1.Enabled = $false
    Start-Sleep -Seconds 2
    $Button1.Enabled = $true
}

$Button1.add_Click(
    {
        Test
    }
)
[void]$Form.ShowDialog()

我尝试在函数中放入[System.Windows.Forms.Application]::DoEvents()button1.Update()
我试过在初始函数中禁用按钮,然后在一个单独的函数中调用需要更长时间的部分,但这也不起作用。即:

Function DisableBtn1 {
    $Button3.Enabled = $false
}

Function DoStuff {
    Write-Host "Bt1 Clicked"
    Start-Sleep -Seconds 2
    $Button3.Enabled = $True

}

$Button1.add_Click(
    {
        DisableBtn3
        DoStuff
    }
)

当脚本运行时,是否有什么明显的地方我没有注意到,使GUI元素被正确禁用?

kt06eoxx

kt06eoxx1#

表单GUI是一个表单GUI,不管它背后的底层代码是什么。没有PowerShell按钮这样的东西,因为PowerShell不是GUI。GUI中发生或设置的任何事情都是通过GUI库进行的。为什么要禁用立即启用功能中的按钮,从而使其再次可用?
做这个简单的测试就能说明我的意思:

Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()

$Form                            = New-Object system.Windows.Forms.Form
$Form.ClientSize                 = '400,400'
$Form.text                       = "Form"
$Form.TopMost                    = $false
$Button1                         = New-Object system.Windows.Forms.Button
$Button1.text                    = "button"
$Button1.width                   = 100
$Button1.height                  = 25
$Button1.enabled                 = $true
$Button1.location                = New-Object System.Drawing.Point(214,59)
$Button1.Font                    = 'Microsoft Sans Serif,10'

$Form.controls.AddRange(@($Button1))

Function Test 
{
    Write-Host 'Clicked'
    $Button1.Enabled = $false
    $Button1.IsAccessible = $false
    Start-Sleep -Seconds 2
    $Button1.Enabled = $true
}

$Button1.add_Click(
    {
        Test
    }
)
[void]$Form.ShowDialog()

不要用enable线

Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()

$Form                            = New-Object system.Windows.Forms.Form
$Form.ClientSize                 = '400,400'
$Form.text                       = "Form"
$Form.TopMost                    = $false
$Button1                         = New-Object system.Windows.Forms.Button
$Button1.text                    = "button"
$Button1.width                   = 100
$Button1.height                  = 25
$Button1.enabled                 = $true
$Button1.location                = New-Object System.Drawing.Point(214,59)
$Button1.Font                    = 'Microsoft Sans Serif,10'

$Form.controls.AddRange(@($Button1))

Function Test 
{
    Write-Host 'Clicked'
    $Button1.Enabled = $false
    Start-Sleep -Seconds 2
}

$Button1.add_Click(
    {
        Test
    }
)
[void]$Form.ShowDialog()

相关问题