WP8 Toolkit PhoneTextBox的密码效果

本文关键字:密码 Toolkit PhoneTextBox WP8 | 更新日期: 2023-09-27 18:04:01

我想在我的WP8项目中添加一个密码文本框。

最明显的方法是PasswordBox;然而,我想包含一个占位符,这似乎是不可能的。

所以我使用了WP8工具包中的PhoneTextBox功能,它允许占位符("提示")。但是,该特性似乎没有办法将该字段指定为密码字段。

我想要的是小圆点而不是字符:)

WP8 Toolkit PhoneTextBox的密码效果

这是我所做的。使用passwordBox实现小圆点的密码掩蔽效果,使用普通文本框显示提示,颜色为浅灰色。

Xaml在这里:

        <!--used a textbox to show watermark and the other is a password box-->
        <TextBox x:Name="PasswordWatermark" TextWrapping="Wrap" Text="{Binding LocalizedResources.Password, Source={StaticResource LocalizedStrings}}" Foreground="{StaticResource PhoneTextBoxReadOnlyBrush}" IsHitTestVisible="False"/>
        <PasswordBox Name="PwdBox" LostFocus="PasswordBox_LostFocus" Opacity="0" GotFocus="PasswordBox_GotFocus" Password="{Binding Password, Mode=TwoWay}" BorderThickness="0" FontFamily="Segoe UI"  KeyDown="PwdBox_KeyDown" PasswordChanged="PwdBox_PasswordChanged"/>

确保你设置了边距,宽度和内边距,使这两个框完全重叠。

下面是代码

    /// <summary>
    /// Function to be executed when focus is on the password box.
    /// Basic function is to show the watermark in the password box.
    /// </summary>
    /// <param name="sender">object</param>
    /// <param name="e">RoutedEventArgs</param>
    private void PasswordBox_LostFocus(object sender, RoutedEventArgs e)
    {
        CheckPasswordWatermark();
    }
    /// <summary>
    /// Code checking the status of the password box and managing the watermark.
    /// </summary>
    private void CheckPasswordWatermark()
    {
        var passwordEmpty = string.IsNullOrEmpty(PwdBox.Password);
        PasswordWatermark.Opacity = passwordEmpty ? 100 : 0;
        PwdBox.Opacity = passwordEmpty ? 0 : 100;
    }
    /// <summary>
    /// Function to be executed when the password box loses focus.
    /// Basic fuction is to show the watermark in the password box.
    /// </summary>
    /// <param name="sender">object</param>
    /// <param name="e">RoutedEventArgs</param>
    private void PasswordBox_GotFocus(object sender, RoutedEventArgs e)
    {
        PasswordWatermark.Opacity = 0;
        PwdBox.Opacity = 100;
        if (!string.IsNullOrEmpty(PwdBox.Password))
        {
            PwdBox.SelectAll();
        }
    }

我就是这么做的

这是一个带水印的密码盒的开源实现:https://github.com/JoshClose/WindowsPhoneControls