如何创建一个只接受数字的输入范围的 PasswordBox?

5
这似乎是Windows Phone中一个明显的疏忽。 <PasswordBox /> 控件不允许指定 InputScope
我需要在自定义密码输入屏幕(例如图像和按钮)中显示 PasswordBox,并显示数字键盘以仅允许数字输入。
Coding4Fun的 PasswordInputPrompt 允许数字输入,但它不能定制,即我无法构建自定义布局,它只允许 TitleMessage 值。
有没有办法让我做到这一点?

http://developer.nokia.com/community/wiki/How_to_create_a_PasswordBox_with_numeric_Input_Scope_on_Windows_Phone - Heena
1
Coding4Fun的库和PasswordInputPrompt是完全开源的,您绝对可以根据自己的需求进行修改和适应。也许如果您指定了自定义布局的要求,您可能会得到一些帮助。 - FunksMaName
@HeenaPatil 你试过那个诺基亚的样例了吗? - Kulasangar
@anonshankar,我已经让诺基亚的例子运行起来了,看起来它很有效。 - DaveDev
3个回答

7

最好自己创建用户控件。

XAML看起来应该是这样的:

<Grid x:Name="LayoutRoot">
    <TextBox x:Name="PasswordTextBox" InputScope="Number" MaxLength="{Binding MaxLength, ElementName=UserControl}" KeyUp="PasswordTextBox_KeyUp"/>
</Grid>

代码可能如下:

public partial class NumericPasswordBox : UserControl
{
    #region Password 

    public string Password
    {
        get { return (string)GetValue(PasswordProperty); }
        set { SetValue(PasswordProperty, value); }
    }

    // Using a DependencyProperty as the backing store for Password.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty PasswordProperty =
        DependencyProperty.Register("Password", typeof(string), typeof(NumericPasswordBox), new PropertyMetadata(null));

    #endregion

    #region MaxLength

    public int MaxLength
    {
        get { return (int)GetValue(MaxLengthProperty); }
        set { SetValue(MaxLengthProperty, value); }
    }

    // Using a DependencyProperty as the backing store for MaxLength.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty MaxLengthProperty =
        DependencyProperty.Register("MaxLength", typeof(int), typeof(NumericPasswordBox), new PropertyMetadata(100));

    #endregion

    public NumericPasswordBox()
    {
        InitializeComponent();
    }

    private void PasswordTextBox_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
    {
        Password = PasswordTextBox.Text;

        //replace text by *
        PasswordTextBox.Text = Regex.Replace(Password, @".", "●");

        //take cursor to end of string
        PasswordTextBox.SelectionStart = PasswordTextBox.Text.Length;
    }
}

从那时起,您可以根据自己的喜好进行所有定制,使用依赖属性(例如在示例中显示的MaxLength)。


3
看代码,这根本行不通。密码属性将被填充为●。 - Igor Kulman
太棒了!它解决了我的问题。谢谢朋友。+1 - Zia Ur Rahman

0

很遗憾,PasswordBox控件没有继承自TextBox控件,所以它不支持InputScope。我认为最好的方法是创建一个新的控件。

我同意你的看法,这有点令人沮丧,因为Windows Phone已经有了那种类型的控件(锁屏密码)。


0

欢迎尝试我的新的易于使用的NumericPassword组件,适用于Windows Phone。

它基于基本的在线示例,但在GUI中具有更好的值编辑支持(例如插入符位置,覆盖选定部分),以提供更好的用户体验。

可以在这里找到一个快速教程。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接