Powershell:WPF在单击第二个按钮时的操作
我正在构建一个可以重新启动Windows机器的小UI。我想让重启按钮提示用户确认,然后在第二次单击时执行重启。我想我会使用一个计数器来实现这个目标,但是看起来按钮并没有更新$counter的值。这是实现这一目标的最佳方式吗Powershell:WPF在单击第二个按钮时的操作,wpf,powershell,Wpf,Powershell,我正在构建一个可以重新启动Windows机器的小UI。我想让重启按钮提示用户确认,然后在第二次单击时执行重启。我想我会使用一个计数器来实现这个目标,但是看起来按钮并没有更新$counter的值。这是实现这一目标的最佳方式吗 Function Start-guitest { [CmdletBinding()] [xml]$inputXML = @" <Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/present
Function Start-guitest {
[CmdletBinding()]
[xml]$inputXML = @"
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp2"
Title="Configuration Script" Height="187.481" Width="400">
<Grid>
<GroupBox x:Name="Actions" Header="Actions" HorizontalAlignment="Left" Height="108" VerticalAlignment="Top" Width="170" Margin="7,11,0,0">
<StackPanel>
<Button x:Name="eventLog_btn" Content="Show errors and warnings"/>
<Label />
<Button x:Name="restart_btn" Content="Restart Windows"/>
</StackPanel>
</GroupBox>
<GroupBox x:Name="Output" Header="Output" HorizontalAlignment="Left" Margin="183,11,0,0" Width="200" Height="108" VerticalAlignment="Top">
<TextBox x:Name="output_txtbx" IsReadOnly="True" HorizontalScrollBarVisibility="Auto" TextWrapping="Wrap" VerticalScrollBarVisibility="Auto" />
</GroupBox>
</Grid>
</Window>
"@
Set-Variable -Name $inputXML -Value ($inputXML.Window -replace 'mc:Ignorable="d"','' -replace "x:N",'N' -replace '^<Win.*', '<Window') -Force -ErrorAction SilentlyContinue
[void][System.Reflection.Assembly]::LoadWithPartialName('PresentationFramework')
[xml]$xaml = $inputXML
# Read XAML.
$reader = (New-Object System.Xml.XmlNodeReader $xaml)
Try {
$form = [Windows.Markup.XamlReader]::Load($reader)
}
Catch {
#error message
}
# Connect to controls.
$xaml.SelectNodes("//*[@*[contains(translate(name(.),'n','N'),'Name')]]") | ForEach {
New-Variable -Name "WPF$($_.Name)" -Value $form.FindName($_.Name) -Force
}
$counter = 0
$WPFeventLog_btn.Add_Click({
Try {
Get-EventLog -LogName Application -After $startTime.AddSeconds(-3) -EntryType Warning,Error -Source $EventLogSource | Out-GridView -Title "Deployment errors.";
$WPFoutput_txtbx.Text = "Please remediate deployment errors, then re-run the config script."
}
Catch {
$WPFoutput_txtbx.Text = "There are no configuration errors or warnings."
}
})
$WPFrestart_btn.Add_Click({
Try {
if ($counter -eq 0) {
$WPFoutput_txtbx.Text = "Are you sure the server is fully configured? Counter is: $counter"
$counter++
} elseif ($counter -gt 0) {
$WPFoutput_txtbx.Text = "Okay, rebooting. Counter is: $counter"
}
}
Catch {
Write-Warning $_
}
})
$form.ShowDialog() | Out-Null
}
您遇到了范围问题。将$counter的所有引用更改为$global:counter,您的问题将得到解决。您拥有的代码中有什么不起作用?第一次单击是否起作用?也就是说,它是否会在文本框中显示该消息,并显示$counter值是多少?是的,但当我再次单击它时,似乎什么都没有发生。当命令位于else语句中时,不会重新绘制文本,也不会重新启动。这是胡说八道。一个完全在函数内部使用的变量怎么会有范围问题?按钮是它自己的作用域吗?我相信$Form在它自己的作用域中起作用。您还可以尝试使用$script:counter来定义限制性更强的作用域,甚至可以设置变量-Name'counter'-scope 1-Value$counter+1来修改父作用域中的变量。是的,脚本作用域也可以工作。问题出现在b/c Add_Click将脚本块添加到一个匿名函数中,该函数有自己的作用域,因此Start guitest中的$counter和$WPFrestart_btn中的$counter.Add_Click{…}是两个不同的变量。话虽如此,我发现计数器递增一次的构造,因此您可以将再次单击同一按钮解释为确认,这有点奇怪。你为什么不直接提示确认呢$rsp=[Windows.Forms.MessageBox]::显示“真的重新启动了吗?”、“确认”、“是否”