我從 Visual Studio 2012 制作了一個 XAML 表單,XAML 表單包含您選擇以獲取資訊的復選框。
每個復選框背后都有一個 powershell 功能。
在執行表單時,如果我選中一個復選框并取消選中它,當我按下按鈕執行該功能時,即使在我按下執行按鈕的那一刻未選中該復選框,它也會繼續認為該復選框已被選中。但我在單擊執行按鈕之前取消選中該復選框。所以它繼續執行我不希望它執行的其他功能。
我們是否有一種代碼可以強制 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
})
}
})
uj5u.com熱心網友回復:
仍在試圖弄清楚您要做什么,因為我們看不到您的其余代碼,但是從您的最新評論中,我認為您在錯誤的事件處理程式中定義了該函式。
取而代之的是$apps_checkbox.Add_Click({..}),您需要檢查該復選框是否在的 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在復選框未選中時禁用該按鈕。這樣您就不需要測驗復選框是否被選中,因為如果未選中,用戶將無法單擊該按鈕。
uj5u.com熱心網友回復:
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
}
})
謝謝你
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/313969.html
下一篇:使用XAML顯示類的所有屬性
