Powershell:第二次按钮点击时的WPF操作

时间:2018-01-26 20:35:35

标签: wpf powershell

我正在构建一个可以重启Windows机器的小UI。我想让重启按钮提示用户进行确认,然后在第二次点击发生时执行重启。我想我会使用计数器来实现这个目标,但看起来按钮不会更新$counter的值。这是实现这一目标的最佳方式吗?

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
}

1 个答案:

答案 0 :(得分:1)

您正在遇到范围问题。将$counter的所有引用更改为$global:counter,您的问题将得到解决。