尝试在图像点击时隐藏密码

时间:2016-10-19 09:57:20

标签: c# xaml uwp uwp-xaml

尝试在点击图片时隐藏密码,代码如下所示。在xaml部分,我已经采用文本框接受密码,后面的代码我编写了隐藏代码但是在提供可见性时出错。 参考图片enter image description here

<TextBox Name="txtBoxPassw"  Height="45" Width="246" InputScope="Password" Margin="10,5,5,5" Background="White" VerticalAlignment="Center"  
                         FontSize="25" FontFamily="Segoe UI Light" Visibility="Visible" Foreground="Black"  Padding="50,5,5,5" TextWrapping="Wrap"
                         BorderThickness="0,0,0,2" BorderBrush="Gray"  />
                <Canvas Margin="58,-45,136,0">
                    <Image x:Name="passLogo" Source="Assets/ic_pass.png" Height="41" Width="41" />
                </Canvas>
                <!--19oct-->
                <TextBlock Name="txtBlockPasswFieldError" Grid.Row="2" Margin="-60,0,0,0" Foreground="Black" Text="*Please enter valid name"
                      HorizontalAlignment="Center" Visibility="Visible" FontSize="11"/>
                <!--19oct-->
                <Image Name="showimg" Source="Assets/show_pass.png"  Width="25" Height="50" Margin="50,15,40,10" Tapped="Image_Tapped" Stretch="Uniform"/>
                <TextBlock Name="showPass"
                    Text="Show Password"
                    Foreground="#303030"  
                    FontSize="15"
                    FontFamily="Koblenz Serial Medium" 
                    Margin="200,-45,15,20" />

cs文件

private void Image_Tapped(object sender, TappedRoutedEventArgs e)
        {

            BitmapImage bitcurrentimage = showimg.Source as BitmapImage;
            //if (showimg.Source == new BitmapImage(new Uri("ms-appx:///Assets/show_pass.png", UriKind.RelativeOrAbsolute)))
            if (bitcurrentimage.UriSource.AbsoluteUri == "ms-appx:///Assets/show_pass.png")
            {
                //passBox.PasswordRevealMode = PasswordRevealMode.Visible;
                txtBoxPassw.Visibility = Visibility.Visible;
                showPass.Text = "Hide Password";
                SetImage("ms-appx:///Assets/hide_pass_.png");
            }
            else
            {
                //passBox.PasswordRevealMode = PasswordRevealMode.Hidden;
                txtBoxPassw.Visibility = Visibility.Visible.Equals(false);
                showPass.Text = "Show Password";
                SetImage("ms-appx:///Assets/show_pass.png");
            }

        }

2 个答案:

答案 0 :(得分:0)

  

实际上我想把验证(如果(Utility.isEmptyString(txtBoxPassw.Text))放在密码箱中输入的字符串上,但是当我尝试它时会给出错误&#34;&#39; PasswordBox&#39;不包含&#39;文字&#39;的定义,并且没有扩展方法&#39;文字&#39;接受类型&#39;密码框&#39;的第一个参数(您是否缺少使用指令或汇编参考?)&#34;

这是您的最后一条评论,我在此评论中假设您接受使用内置控件PasswordBox而非创建自己的控件的方法。

然后您可以引用PasswordBox class,与TextBoxTextBlock不同,PasswordBox没有名为&#34; Text&#34;的属性。我认为你需要的是PasswordBox.Password property

例如:

<PasswordBox x:Name="passwb" Height="30" IsPasswordRevealButtonEnabled="True" />
<Button Content="click" VerticalAlignment="Bottom" Click="Button_Click"  />

代码背后:

private void Button_Click(object sender, RoutedEventArgs e)
{
    var pass = passwb.Password;
}

此处的pass表示PasswordBox当前持有的密码,该密码名为&#34; passwb&#34;。

答案 1 :(得分:-1)

在Winform中有一个文本框参数&#39; PasswordChar&#39;,如果将其设置为一个字符(例如*),它会将文本框中的所有字符替换为该字符。如果您将passwordChar设置为&#39; \ o&#39; (PasswordBox.PasswordChar =&#39; \ 0&#39;;)它将显示所有正常的字符。我希望在UWP中有类似的东西 - 我不是UWP。