2016-06-01 4 views
0

私はPowershellで私の最初のフォームに取り組んでおり、XAML(https://foxdeploy.com/2015/04/10/part-i-creating-powershell-guis-in-minutes-using-visual-studio-a-new-hope/のコードを基にしています)を使っています。XAMLフォーム+キャンセルボタンの応答

フォームは素晴らしいので、データ検証に取り組んでいます。私は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' 
    } 
} 

答えて

0

イベントハンドラを追加して変数を設定し、それに基づいてループを壊しました。

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