执行时 Powershell 和 XAML 复选框问题

Powershell and XAML checkboxes problem while executing

我从 visual studio 2012 年制作了一个 XAML 表格,XAML 表格有复选框,您 select 可以获取信息。

每个复选框背后都有一个 powershell 函数。

执行表单时,如果我select一个复选框并取消select它,当我按下按钮执行该功能时,即使在我按下时取消选中复选框执行按钮它继续认为复选框是 selected。但是我在单击执行按钮之前取消选中该复选框。所以它继续执行我不希望它执行的其他功能。

我们是否有一种代码可以强制 PowerShell 在执行表单时将复选框视为 $false?

这里有一个代码示例:

 $apps_checkbox = $Window.FindName("apps_checkbox")
    $apps_checkbox.Add_Click({ 
    if($apps_checkbox.isChecked -eq $true){
       $retrievebutton.add_click({
       IF(!(GCI ".$FOLDERNAME$COMP" | WHERE-OBJECT NAME -EQ "APPS")){ 
        NEW-ITEM -ITEMTYPE DIRECTORY -NAME APPS -PATH ".$FOLDERNAME$COMP" 
       }
       NEW-ITEM -ITEMTYPE FILE -PATH 
       ".$FOLDERNAME$COMP\APPS\APPS-$COMP.TXT" 
       LIST-APPS 
        }) 
       } 
      })

仍在尝试弄清楚您要做什么,因为我们看不到您的其余代码,但根据您最近的评论,我了解到您在错误的事件处理程序中定义了函数。

而不是 $apps_checkbox.Add_Click({..}),您需要检查该复选框是否被选中 $retrievebutton 的点击事件中:

$retrievebutton = $Window.FindName("retrievebutton")
$retrievebutton.Add_Click({ 
    $apps_checkbox = $Window.FindName("apps_checkbox")
    if($apps_checkbox.IsChecked){
        # No need to test using -Force. Also throw away the diretoryInfo object it returns
        # You may need to use 'Script-scoping' here on the variables $FOLDERNAME and $COMP
        # like $script:FOLDERNAME and $script:COMP
        $null = New-Item -Path ".$FOLDERNAME$COMP\APPS" -ItemType Directory -Force
        $null = New-Item -Path ".$FOLDERNAME$COMP\APPS\APPS-$COMP.TXT" -ItemType File -Force
        # call your function 
        LIST-APPS 
    }
})

另一种选择是在复选框未选中时禁用 $retrievebutton 按钮。这样你就不需要测试复选框是否被选中,因为如果没有被选中,用户将无法点击按钮。

Theo 是对的:下面的代码解决了问题:

$retrievebutton = $Window.FindName("retrievebutton")
$retrievebutton.Add_Click({ 
    $apps_checkbox = $Window.FindName("apps_checkbox")
    if($apps_checkbox.IsChecked){
        # No need to test using -Force. Also throw away the diretoryInfo object it returns
        # You may need to use 'Script-scoping' here on the variables $FOLDERNAME and $COMP
        # like $script:FOLDERNAME and $script:COMP
        $null = New-Item -Path ".$FOLDERNAME$COMP\APPS" -ItemType Directory -Force
        $null = New-Item -Path ".$FOLDERNAME$COMP\APPS\APPS-$COMP.TXT" -ItemType File -Force
        # call your function 
        LIST-APPS 
    }
})

谢谢