如何使用powershell为passwordBox[WPF]的PasswordRevealMode设置属性
我想知道如何使用复选框在带有powershell(使用WPF)的密码框中显示或隐藏密码。我找到了很多关于c#的例子,但我无法让它与powershell一起工作。如何将powershell中的paswordrevealmode属性设置为“隐藏”或“可见” 在c#中,这是这样做的:如何使用powershell为passwordBox[WPF]的PasswordRevealMode设置属性,powershell,properties,passwordbox,Powershell,Properties,Passwordbox,我想知道如何使用复选框在带有powershell(使用WPF)的密码框中显示或隐藏密码。我找到了很多关于c#的例子,但我无法让它与powershell一起工作。如何将powershell中的paswordrevealmode属性设置为“隐藏”或“可见” 在c#中,这是这样做的: private void CheckBox_Changed(object sender, RoutedEventArgs e) { if (revealModeCheckBox.IsChecked ==
private void CheckBox_Changed(object sender, RoutedEventArgs e)
{
if (revealModeCheckBox.IsChecked == true)
{
passwordBox1.PasswordRevealMode = PasswordRevealMode.Visible;
}
else
{
passwordBox1.PasswordRevealMode = PasswordRevealMode.Hidden;
}
}
我知道我在这里遗漏了一些东西,我仍在使用powershell学习wpf:)
我使用的代码如下所示:
function Get-Info () {
[void][System.Reflection.Assembly]::LoadWithPartialName('presentationframework')
[xml]$XAML = @'
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Informations requises" Height="161.4" Width="544.4">
<Grid>
<Label Name="label" Content="Est-ce que le serveur *** est local, saas ou cloud ?" HorizontalAlignment="Left" Margin="13,6,0,0" VerticalAlignment="Top" Foreground="#FF1843BA" FontWeight="Bold" Width="296" />
<Label Name="label1" Content="Mot de passe genere dans *** :" HorizontalAlignment="Left" Height="26" Margin="12,33,0,0" VerticalAlignment="Top" Width="245" RenderTransformOrigin="0.5,0.5" Foreground="#FF1843BA" FontWeight="Bold">
<Label.RenderTransform>
<TransformGroup>
<ScaleTransform />
<SkewTransform />
<RotateTransform Angle="0.035" />
<TranslateTransform />
</TransformGroup>
</Label.RenderTransform>
</Label>
<TextBox Name="tbIndex" HorizontalAlignment="Left" Height="23" Margin="94,62,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="203" />
<Label Name="label2" Content="Index Client:" HorizontalAlignment="Left" Margin="13,60,0,0" VerticalAlignment="Top" Foreground="#FF1843BA" FontWeight="Bold" />
<Label Name="label3" Content="Adresse IP:" HorizontalAlignment="Left" Margin="13,91,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.202,0.727" Foreground="#FF1843BA" FontWeight="Bold" />
<Label Name="label7" Content="DNS Restreint ?" HorizontalAlignment="Left" Margin="302,60,0,0" VerticalAlignment="Top" Foreground="#FF1843BA" FontWeight="Bold" />
<Button Name="btSave" Content="Enregistrer les informations" HorizontalAlignment="Left" Margin="326,91,0,0" VerticalAlignment="Top" Width="164" RenderTransformOrigin="0.501,-0.221" Height="23" />
<StackPanel Name="spServerType" Height="28" Margin="306,7,-18.8,0" Width="212" Orientation="Horizontal" VerticalAlignment="Top" HorizontalAlignment="Left">
<RadioButton Name="rbCloud" Content="Cloud" HorizontalAlignment="Left" VerticalAlignment="Top" GroupName="Serveur" Margin="5" />
<RadioButton Name="rbServerLocal" Content="Serveur Local" HorizontalAlignment="Left" Height="22" VerticalAlignment="Top" Width="91" GroupName="Serveur" Margin="5" />
<RadioButton Name="rbSaas" Content="SaaS" HorizontalAlignment="Left" VerticalAlignment="Top" GroupName="Serveur" Margin="5" />
</StackPanel>
<StackPanel Name="spDNS" HorizontalAlignment="Left" Height="24" Margin="405,61,0,0" VerticalAlignment="Top" Width="101" Orientation="Horizontal">
<RadioButton Name="rbDnsYes" Content="Oui" HorizontalAlignment="Left" VerticalAlignment="Top" GroupName="DNS" Margin="5" />
<RadioButton Name="rbDnsNo" Content="Non" HorizontalAlignment="Left" VerticalAlignment="Top" GroupName="DNS" Margin="5" />
</StackPanel>
<TextBox Name="tbIP" HorizontalAlignment="Left" Height="24" Margin="94,96,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="204" />
<StackPanel Name="spPassword" Margin="254,32,8.4,67" Orientation="Horizontal">
<PasswordBox Name="pbPassword" Width="245" Margin="0,1,0,1" />
<CheckBox Name="revealModeCheckBox" Content="" IsChecked="False" ToolTip="Show password" Margin="6,6" />
</StackPanel>
</Grid>
</Window>
'@
#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. Some possible causes for this problem include: .NET Framework is missing PowerShell must be launched with PowerShell -sta, invalid XAML code was encountered."; exit}
$xaml.SelectNodes("//*[@Name]") | %{Set-Variable -Name ($_.Name) -Value $Form.FindName($_.Name)}
## Item1
$revealModeCheckBox.Add_Checked({
#***this is the part i have trouble with***
$pbPassword.PasswordRevealMode = "PasswordRevealMode.Visible"
})
$btSave.Add_Click({
$global:hastable = @{}
#get the password as SecureString
$hastable.add( "Password", ($pbPassword.securePassword | ConvertFrom-SecureString))
$form.Close()
})
$Form.ShowDialog() | out-null
return $Global:hastable
}
get-info
或
在powershell中设置此属性似乎不是正确的方法。。我在执行此操作时出现以下错误:
The property 'PasswordRevealMode' cannot be found on this object. Verify that the property exists and can be set.
At C:\git\temp\Samplescript.ps1:54 char:5
+ $pbPassword.PasswordRevealMode = "PasswordRevealMode.Visible"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyAssignmentException
或
或
实际的GUI如下所示:
private void CheckBox_Changed(object sender, RoutedEventArgs e)
{
if (revealModeCheckBox.IsChecked == true)
{
passwordBox1.PasswordRevealMode = PasswordRevealMode.Visible;
}
else
{
passwordBox1.PasswordRevealMode = PasswordRevealMode.Hidden;
}
}
我发现了一种使用弹出消息来显示密码的方法,但我尝试使用密码框来显示密码,如下所述:
提前谢谢 因此,我查看了文档,默认情况下,passwordbox控件上应该有一个允许显示的按钮。如果你的没有,那么还有别的事情在发生。首先,您是否在代码中显式导入了Windows.UI.Xaml.Controls?我不这么认为。。我认为它是presentationframework的一部分,那么如何导入它?@jiciftw我认为这是不可能的,因为WPF PasswordBox中没有PasswordRevealMode属性。该属性似乎适用于通用Windows平台应用程序,而不是Powershell GUI。当我尝试强制此属性时,它会说,
System.Windows.Controls.PasswordBox.PasswordRevealMode
未知。对于Windows窗体中的常规文本框,您可以使用字符来屏蔽实际输入字符以获得屏蔽密码输入。使用密码框对安全性不是更好吗?密码以securestring的形式给出,这与使用文本框然后将其转换为安全字符串有区别吗?
The property 'PasswordRevealMode' cannot be found on this object. Verify that the property exists and can be set.
At C:\git\temp\Samplescript.ps1:54 char:5
+ $pbPassword.PasswordRevealMode = "PasswordRevealMode.Visible"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyAssignmentException
The property 'PasswordRevealMode' cannot be found on this object. Verify that the property exists and can be set.
At C:\git\temp\Samplescript.ps1:54 char:5
+ $pbPassword.PasswordRevealMode = 1
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyAssignmentException
The property 'PasswordRevealMode' cannot be found on this object. Verify that the property exists and can be set.
At C:\git\temp\Samplescript.ps1:54 char:5
+ $pbPassword.PasswordRevealMode = $true
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyAssignmentException