XAML表单+取消按钮响应

时间:2016-06-01 01:53:13

标签: xaml powershell

我正在使用Powershell处理我的第一个表单并使用XAML(基于https://foxdeploy.com/2015/04/10/part-i-creating-powershell-guis-in-minutes-using-visual-studio-a-new-hope/的代码构建)。

表单效果很好所以现在我正在进行数据验证。我试图通过While循环强制所有字段,但我不能打破它。当有人点击“取消”按钮时,我如何获取下面的代码并使其理解,它应该退出循环?我尝试检查$ WPFcancelButton.IsPressed,但似乎没有得到更新。

感谢。

Function Get-HubNameFields {
$inputXML = @"
<Window x:Class="WpfApplication1.MainWindow"
        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:WpfApplication1"
        mc:Ignorable="d"
        Title="Naming Tool" Height="350" Width="525">
    <Grid Margin="0,0,-8,0" HorizontalAlignment="Left" Width="525">
        <TextBlock x:Name="textBlock" Margin="10,10,145.907,0" TextWrapping="Wrap" Text="Define the parameters of a name" VerticalAlignment="Top" Height="17.945"/>
        <Label x:Name="custLongNameLabel" Content="Customer Long Name" HorizontalAlignment="Left" Margin="10,32.945,0,0" VerticalAlignment="Top"/>
        <TextBox x:Name="custLongNameTxt" Height="21.96" Margin="141.003,36.945,224.239,0" TextWrapping="Wrap" VerticalAlignment="Top" ToolTip="Full name of the customer."/>
        <Label x:Name="custShortNameLabel" Content="Customer Short Name" HorizontalAlignment="Left" Margin="10,59.905,0,0" VerticalAlignment="Top"/>
        <TextBox x:Name="custShortNameTxt" Height="21.96" Margin="141.003,63.905,224.239,0" TextWrapping="Wrap" VerticalAlignment="Top" ToolTip="Abreviation of the customer's name."/>
        <Label x:Name="siteLongNameLabel" Content="Site Long Name" HorizontalAlignment="Left" Margin="10,86.865,0,0" VerticalAlignment="Top"/>
        <TextBox x:Name="siteLongNameTxt" Height="21.96" Margin="141.003,90.865,224.239,0" TextWrapping="Wrap" VerticalAlignment="Top" ToolTip="Full name of the site."/>
        <Label x:Name="siteShortNameLabel" Content="Site Short Name" HorizontalAlignment="Left" Margin="10,113.785,0,0" VerticalAlignment="Top"/>
        <TextBox x:Name="siteShortNameTxt" Height="21.96" Margin="141.003,117.825,224.239,0" TextWrapping="Wrap" VerticalAlignment="Top" ToolTip="Abbreviation of the site's name."/>
        <Label x:Name="hubNumberLabel" Content="Hub Number" HorizontalAlignment="Left" Margin="10,140.825,0,0" VerticalAlignment="Top"/>
        <TextBox x:Name="hubNumberTxt" Height="21.96" Margin="141.003,144.785,224.239,0" TextWrapping="Wrap" VerticalAlignment="Top" ToolTip="Number of the Hub."/>
        <RadioButton x:Name="radioButton" Content="Data Center Hub" HorizontalAlignment="Left" Margin="10,0,0,122.611" VerticalAlignment="Bottom"/>
        <RadioButton x:Name="radioButton1" Content="Office/Other" HorizontalAlignment="Left" Margin="10,0,0,102.651" VerticalAlignment="Bottom"/>
        <Button x:Name="okButton" Content="OK" HorizontalAlignment="Left" Margin="18.928,0,0,10" VerticalAlignment="Bottom" Width="50" IsDefault="True"/>
        <Button x:Name="cancelButton" Content="Cancel" HorizontalAlignment="Left" Margin="80.317,0,0,10" VerticalAlignment="Bottom" Width="50" IsCancel="True"/>
    </Grid>
</Window>
"@

    $inputXML = $inputXML -replace 'mc:Ignorable="d"','' -replace "x:N",'N' -replace '^<Win.*', '<Window'

    [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 {
        Write-Host "Unable to load Windows.Markup.XamlReader. Double-check syntax and ensure .Net is installed."
    }

    #This line takes the XAML data, adds "WPF" to the front of each XML node name, and creates a global variable.
    $xaml.SelectNodes("//*[@Name]") | Foreach {Set-Variable -Name "WPF$($_.Name)" -Value $Form.FindName($_.Name) -Scope Global}

    <#Only need this function and its call if you don't know the name of the variables that the function generates from the XML.
    Function Get-FormVariables {
        If ($global:ReadmeDisplay -ne $true) {
            Write-host "If you need to reference this display again, run Get-FormVariables" -ForegroundColor Yellow;$global:ReadmeDisplay=$true
        }

        Write-host "Found the following interactable elements from our form" -ForegroundColor Cyan

        Get-Variable WPF*
    }

    Get-FormVariables#>

    #Specify what the OK button should do, when clicked.
    $WPFokButton.Add_Click({$form.Close()})

    #Now that the form is built, show it, surpressing other messages.
    $form.ShowDialog() | Out-Null
}

#Initialize some local variables.
$hubType = $null
$TextInfo = (Get-Culture).TextInfo

While ((($customerLongName.Length -eq 0) -or ($customerShortName.Length -eq 0) -or ($siteLongName.Length -eq 0) -or ($siteShortName.Length -eq 0) -or ($hubType -eq $null)) -or ($WPFcancelButton.IsPressed -like "false*")) {
    Write-Host "the cancel button value is $($WPFcancelButton.ispressed)"
    Get-HubNameFields

    $customerLongName = $WPFcustLongNameTxt.Text
    $customerLongName = $TextInfo.ToTitleCase($customerLongName.ToLower())
    $customerLongName = $customerLongName -replace '\s',''

    $customerShortName = ($WPFcustShortNameTxt.Text).ToLower()
    $customerShortName = $customerShortName -replace '\s',''

    $siteLongName = $WPFsiteLongNameTxt.Text
    $siteLongName = $TextInfo.ToTitleCase($siteLongName.ToLower())
    $siteLongName = $siteLongName -replace '\s',''

    $siteShortName = ($WPFsiteShortNameTxt.Text).ToLower()
    $siteShortName = $siteShortName -replace '\s',''

    If ($WPFradioButton.IsChecked -eq $true) {
        $hubType = 'dh'
    }
    If ($WPFradioButton1.IsChecked -eq $true) {
        $hubType = 'ch'
    }
}

1 个答案:

答案 0 :(得分:0)

我添加了一个事件处理程序,用于设置变量,并基于此打破循环。

$WPFcancelButton.Add_Click({Set-Variable -Name UserCancelled -Value $true -Scope Global})
相关问题