我试图使用WPF显示一个窗口,让用户在我的PowerShell脚本中输入密码,窗口工作,但按钮不响应...
主窗口(也使用WPF)工作正常,我只有这个奇怪的行为与这一个。
XAML代码:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
WindowStartupLocation="CenterScreen"
WindowStyle="None"
MinWidth="200" MaxWidth="400" Width="300"
MinHeight="100" MaxHeight="120" Height="110"
FontFamily="Segoe UI" FontSize="13">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Label Name="Label" Grid.Row="0" Content="Enter vault master key:" VerticalContentAlignment="Center" Margin="10,0,0,0"/>
<PasswordBox Name="Input" Grid.Row="1" BorderBrush="Gray" BorderThickness="0.5" Margin="10,5,10,5" />
<StackPanel Grid.Row="2" Orientation="Horizontal" HorizontalAlignment="Center">
<Button Name="Ok" Content="Ok" Width="70" Height="20" Margin="0,0,5,0"/>
<Button Name="Exit" Content="Exit" Width="70" Height="20" Margin="5,0,0,0"/>
</StackPanel>
</Grid>
</Window>
PowerShell代码:
[xml]$xaml = (Get-Content -Path $Files.Inp)
$xamlReader = New-Object System.Xml.XmlNodeReader $xaml
$UserInput = @{}
$UserInput.Window = [Windows.Markup.XamlReader]::Load($xamlReader)
$Controls = [regex]::Matches((Get-Content $Files.Inp -Raw),'Name="(\w+)"')
$Controls.Groups | Where-Object Name -eq 1 | ForEach-Object{ $UserInput.($_.Value) = $UserInput.Window.FindName($_.Value) }
$UserInput.Window.ShowDialog() | Out-Null
$UserInput.Ok.Add_Click({
$UserInput.Input.Password
$UserInput.Window.Close()
})
$UserInput.Exit.Add_Click({ $UserInput.Window.Close() })
当我运行脚本时,窗口显示,我可以在密码框中写入,但两个按钮(确定/退出)都不工作(当我点击时没有附加任何东西)。我检查了控件的名称和方法我使用(Add_Click),一切似乎都是正确的...
你能帮我吗?
1条答案
按热度按时间tuwxkamq1#
你必须移动:
写在剧本的最后