【问题标题】:How can I create a PasswordBox with an input scope that accepts only numbers?如何使用仅接受数字的输入范围创建 PasswordBox?
【发布时间】:2014-08-24 08:17:10
【问题描述】:

这在 Windows Phone 中似乎是一个明显的疏忽。 <PasswordBox /> 控件不允许指定 InputScope

我需要将PasswordBox 显示为自定义密码输入屏幕(例如图像和按钮)的一部分,并显示数字小键盘以仅允许数字输入。

Coding4Fun 的 PasswordInputPrompt 允许数字输入,但它不可自定义,因为我无法围绕它构建自定义布局,它只允许 TitleMessage 值。

我有什么办法可以做到这一点吗?

【问题讨论】:

  • Coding4Fun 的库和 PasswordInputPrompt 是完全开源的,您绝对可以根据需要对其进行修改和调整。也许如果您指定了您的自定义布局应该包含什么,您可能会得到一些帮助。
  • @HeenaPatil 你试过诺基亚的样品吗?
  • @anonshankar 我得到了诺基亚示例工作.. 它似乎在做的伎俩。

标签: c# xaml windows-phone-7 windows-phone-8


【解决方案1】:

最好的办法就是创建自己的用户控件。

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 等依赖属性自定义所有您想要的内容。

【讨论】:

  • 看代码,这根本行不通。密码属性将填入 ●
  • 这太棒了!它解决了我的问题。谢谢哥们 。 +1
【解决方案2】:

很遗憾,PasswordBox 控件不继承自 TextBox 控件,因此它不支持 InputScope。 我认为对您来说最好的方法是创建一个新控件。

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

【讨论】:

    【解决方案3】:

    请随时试用我的新的易于使用的 Windows Phone 组件NumericPassword

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

    可以在here找到快速教程。

    【讨论】:

      猜你喜欢
      • 2022-01-09
      • 2021-10-22
      • 1970-01-01
      • 2021-08-14
      • 2019-12-05
      • 2016-10-08
      • 2017-05-18
      • 2014-06-26
      • 2013-01-14
      相关资源
      最近更新 更多