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})