【问题标题】:What's the proper way to filter characters in a UWP PasswordBox?在 UWP PasswordBox 中过滤字符的正确方法是什么?
【发布时间】:2019-12-01 19:37:16
【问题描述】:

我有一个 UWP 应用程序,它使用 PasswordBox 控件来输入模糊文本。密码的有效字符因实例而异,并且 ViewModel 层在运行时知道。我希望能够在用户键入无效字符时过滤它们。这样做的正确方法是什么?

我们对常规 TextBox 控件有类似的要求。我们已经能够使用 TextChanging 事件和用于重置光标位置的 SelectionStart 属性对文本框执行这种过滤。但是 PasswordBox 没有这些。

密码框的 XAML 如下所示

    <PasswordBox
        x:Name="ThePasswordBox"
        Grid.Row="1"
        MaxLength="{Binding MaxPasswordLength, Mode=OneTime}"
        IsEnabled="{Binding IsPasscodeEnabled, Mode=OneWay}"
        Password="{Binding Password, FallbackValue=1234, Mode=TwoWay}"
        PlaceholderText= "{Binding PlaceholderText, Mode=OneWay}" />

然后我们通过检查输入的有效性来响应 Password.set,如果无效,则重置为先前的值

        set
        {
            if (_password != value && value != null)
            {
                // verify that the new password would be valid; if not, roll back
                if (!IsPasswordContentAcceptable(value))
                {
                    _passcodeControl.SetPassword(_password);
                    return;
                }

                _password = value;

                // push the new password to the data binding
                _passcodeDataBinding.SetCurrentValue(_password);

                // update the UI
                HandlePasswordChange();

                OnPropertyChanged("Password");
            }
        }

对 SetCurrentValue() 的调用只是将输入的密码存储到我们的模型层中,不应成为本次讨论的结果。对 _passwordControl.SetPassword 的调用会更新 ThePasswordBox 上的密码字段:

    public void SetPassword(string password)
    {
        ThePasswordBox.Password = password;
    }

HandlePasswordChange() 强制其他 UI 元素重新评估,包括在控件无效时禁用的 OK 按钮。它的实现对这个问题应该不重要。

这种方法的问题在于,当我们重置 PasswordBox 的内容(调用 SetPassword,设置 PasswordBox.Password 属性)时,光标会跳转到第一个位置。因此,对于数字密码,输入“12a4”将产生“412”。

我们有哪些选择?

【问题讨论】:

  • 您是否尝试这样做以创建密码?这通常不是人们所做的......只需在他们提交时告诉他们他们的密码无效。不要通过删除字符来混淆用户...如果密码只需为数字,您可以将键盘类型设置为数字。
  • 是的,用于创建密码。计划 B 是按照您的建议进行,在提交时进行验证,但当前的设计是在输入错误字符时过滤掉它们。密码有时都是数字,但并非总是如此,所以我们需要一个通用键盘(应用程序也需要在 PC 上运行)
  • 我无法用上面的代码重现您的问题,它错过了_passcodeControl.SetPassword 和_passcodeDataBinding.SetCurrentValue 方法。请为我们分享minimal reproducible example。
  • @RichS 您如何要求用户输入他们看不到的密码并从他们输入的内容中删除字符?还是让他们看到他们正在输入的内容?
  • @visc - 我们在密码控件上有一个“显示”按钮,允许他们查看他们输入的字符

标签: c# xaml uwp passwordbox


【解决方案1】:

在 UWP PasswordBox 中过滤字符的正确方法是什么?

从您的代码中,您无需在Password.set 中调用SetPassword 方法,它会使光标回到起始位置。更好的方法是在双向模式下绑定Password。并检查密码是否可用。如果Password 包含不允许的字符,则使用InputInjector 调用BackSpace。

public string PassWord
{

    get { return _passWord; }
    set
    {
        if(value != null && IsPasswordContentAcceptable(value))
        {
            _passWord = value;
            OnPropertyChanged();
        }
        else
        {
            InputInjector inputInjector = InputInjector.TryCreate();
            var info = new InjectedInputKeyboardInfo();
            info.VirtualKey = (ushort)(VirtualKey.Back);
            inputInjector.InjectKeyboardInput(new[] { info });               
        }

    }
}

Xaml

<PasswordBox
    x:Name="ThePassWordBox"
    MaxLength="20"
    Password="{x:Bind PassWord, Mode=TwoWay}"      
    PlaceholderText="Input your Password"
    />

【讨论】:

  • 谢谢。我以前没有遇到过 InputInjector。我会试一试并报告。
  • 看起来完全符合预期 - 再次感谢您的解决方案,否则我永远不会找到它!
【解决方案2】:

或者,您可以在收到输入时禁止输入。 在这个例子中,除了“A”之外的所有字符都可以在 PasswordBox 中使用

Xaml

    <PasswordBox Name="pwBox"></PasswordBox>

代码

public sealed partial class MainPage : Page
{
    public MainPage()
    {
        CoreWindow cw = CoreWindow.GetForCurrentThread();
        CoreDispatcher cd = cw.Dispatcher;

        cd.AcceleratorKeyActivated += Cd_AcceleratorKeyActivated;
        this.InitializeComponent();
    }

    private void Cd_AcceleratorKeyActivated(CoreDispatcher sender, AcceleratorKeyEventArgs args)
    {
        if (this.pwBox.FocusState != FocusState.Unfocused)
        {
            if(args.VirtualKey == Windows.System.VirtualKey.A)
            {
                args.Handled = true;
            }
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-04
    • 1970-01-01
    • 1970-01-01
    • 2021-06-30
    相关资源
    最近更新 更多