我正在编写一个应用程序,我需要有一个用户输入对话框。我遇到的问题是在用户点击按钮后将文本框中的值转换为变量。
[xml]$XAML_ConnectDialog = @"
<Window Name="Form_ConnectDialog"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Connect" Height="176" Width="328" ResizeMode="NoResize" ShowInTaskbar="False">
<Grid>
<Grid.Background>
<LinearGradientBrush EndPoint="0.5,1" MappingMode="RelativeToBoundingBox" StartPoint="0.5,0">
<GradientStop Color="#FFB4B4B4" Offset="1"/>
<GradientStop Color="White" Offset="0.603"/>
</LinearGradientBrush>
</Grid.Background>
<TextBlock TextWrapping="Wrap" Text="Enter the hostname or IP address of the remote host to connect to." Margin="10,0,10,91"/>
<Label Content="Connect To:" HorizontalAlignment="Left" Height="27" VerticalAlignment="Top" Width="76" Margin="10,47,0,0"/>
<Button Name="Btn_ConnectDialog_Connect" Content="Connect" Height="20" Margin="194,88,26,20" IsDefault="True"/>
<TextBox Name="Txt_ConnectDialog_Input" HorizontalAlignment="Left" Height="23" Margin="91,51,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="208"/>
</Grid>
</Window>
"@
$XML_Node_Reader_ConnectDialog = (New-Object System.Xml.XmlNodeReader $XAML_ConnectDialog)
$ConnectDialog = [Windows.Markup.XamlReader]::Load($XML_Node_Reader_ConnectDialog)
$Btn_ConnectDialog_Connect = $ConnectDialog.FindName('Btn_ConnectDialog_Connect')
$Txt_ConnectDialog_Input = $ConnectDialog.FindName('Txt_ConnectDialog_Input')
$Btn_ConnectDialog_Connect.Add_Click({
$ConnectDialog.Hide()
$var = $Txt_ConnectDialog_Input.Text.ToString()
})
write-host $var
$ConnectDialog.ShowDialog() | Out-Null
感谢任何帮助。
由于
答案 0 :(得分:2)
Write-Host $var
。由于GUI首先用于定义变量,因此Write-Host
命令永远不会返回任何内容。$var
。 eventhandler是一个函数,因此变量将在仅存在于函数内部的局部作用域中创建,然后在函数/ eventhandler完成后删除。我已经通过指定eventhandler来修复此问题,以便将值存储在$var
范围内的变量script
中,因此它将在脚本完成之前可用。修正脚本:
[xml]$XAML_ConnectDialog = @"
<Window Name="Form_ConnectDialog"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Connect" Height="176" Width="328" ResizeMode="NoResize" ShowInTaskbar="False">
<Grid>
<Grid.Background>
<LinearGradientBrush EndPoint="0.5,1" MappingMode="RelativeToBoundingBox" StartPoint="0.5,0">
<GradientStop Color="#FFB4B4B4" Offset="1"/>
<GradientStop Color="White" Offset="0.603"/>
</LinearGradientBrush>
</Grid.Background>
<TextBlock TextWrapping="Wrap" Text="Enter the hostname or IP address of the remote host to connect to." Margin="10,0,10,91"/>
<Label Content="Connect To:" HorizontalAlignment="Left" Height="27" VerticalAlignment="Top" Width="76" Margin="10,47,0,0"/>
<Button Name="Btn_ConnectDialog_Connect" Content="Connect" Height="20" Margin="194,88,26,20" IsDefault="True"/>
<TextBox Name="Txt_ConnectDialog_Input" HorizontalAlignment="Left" Height="23" Margin="91,51,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="208"/>
</Grid>
</Window>
"@
$XML_Node_Reader_ConnectDialog = (New-Object System.Xml.XmlNodeReader $XAML_ConnectDialog)
$ConnectDialog = [Windows.Markup.XamlReader]::Load($XML_Node_Reader_ConnectDialog)
$Btn_ConnectDialog_Connect = $ConnectDialog.FindName('Btn_ConnectDialog_Connect')
$Txt_ConnectDialog_Input = $ConnectDialog.FindName('Txt_ConnectDialog_Input')
$Btn_ConnectDialog_Connect.Add_Click({
$ConnectDialog.Hide()
$script:var = $Txt_ConnectDialog_Input.Text.ToString()
})
$ConnectDialog.ShowDialog() | Out-Null
write-host $var