0

嗨,有人可以帮我吗?我的任务是修复 WPF 中登录过程中的错误(我以前没有经验!)问题似乎是 PasswordBox 控件忽略了输入密码中的零。我检查了密码何时更改并检查了输入的值 - 它肯定会忽略“0”字符。示例密码应该是“password012”,但从 Passwordbox 返回的是“password12”,类似于“0password”返回“password”。用户名条目(TextBox 控件)似乎很好。

有人可以建议如何克服这个问题吗?xaml 代码在这里

<TextBox VerticalAlignment="Center" HorizontalAlignment="Center" Margin="0,10,0,48" Foreground="DarkSlateGray" Grid.Column="1" Grid.Row="1" Text="{Binding Username}" Name="txtUsername" Height="26" HorizontalContentAlignment="Center" Style="{StaticResource RoundTextBoxStyle}" Width="200" IsUndoEnabled="False" Effect="{StaticResource TextBoxDropShadow}" FontFamily="{StaticResource TextBoxDescrptionFontFamily}" FontSize="{StaticResource TextBoxDescriptionFontSize}" GotFocus="TxtBoxGotFocus" ToolTip="Enter your Username." Background="{StaticResource TextBoxBackground}"/>
<PasswordBox Grid.Column="1" Foreground="#FF2F4F4F" Margin="0,0,0,-36" VerticalAlignment="Center" Grid.Row="1" HorizontalAlignment="Center" Height="26" HorizontalContentAlignment="Center"  MaxLength="0" w:PasswordBoxBinder.Attach="True" Style="{StaticResource RoundTextBoxStyle}" w:PasswordBoxBinder.Password="{Binding Path=Password, Mode=TwoWay}" Effect="{StaticResource TextBoxDropShadow}" Width="{Binding ElementName=txtUsername, Path=Width}" ToolTip="Enter your Password." GotFocus="PbxGotFocus" FontFamily="{StaticResource TextBoxDescrptionFontFamily}" FontSize="{StaticResource TextBoxDescriptionFontSize}" Background="{StaticResource TextBoxBackground}" PasswordChanged="PasswordBox_PasswordChanged"  />

PasswordBox_PasswordChanged 的​​代码是我自己添加的,用于调试目的,以了解发生了什么。

    private void PasswordBox_PasswordChanged(object sender, RoutedEventArgs e)
    {
        // ... Display Password in Title.
        //     A bad design decision.
        var box = sender as PasswordBox;
        this.Title = "Password typed: " + box.Password;
    }

PasswordBoxBinder 的代码在这里,是原始代码的一部分: public static class PasswordBoxBinder {

    //test
    public static readonly DependencyProperty PasswordProperty =
        DependencyProperty.RegisterAttached("Password",
        typeof(string), typeof(PasswordBoxBinder),
        new FrameworkPropertyMetadata(string.Empty, OnPasswordPropertyChanged));

    public static readonly DependencyProperty AttachProperty =
        DependencyProperty.RegisterAttached("Attach",
        typeof(bool), typeof(PasswordBoxBinder), new PropertyMetadata(false, Attach));

    private static readonly DependencyProperty IsUpdatingProperty =
       DependencyProperty.RegisterAttached("IsUpdating", typeof(bool),
       typeof(PasswordBoxBinder));


    public static void SetAttach(DependencyObject dp, bool value)
    {
        dp.SetValue(AttachProperty, value);
    }

    public static bool GetAttach(DependencyObject dp)
    {
        return (bool)dp.GetValue(AttachProperty);
    }

    public static string GetPassword(DependencyObject dp)
    {
        return (string)dp.GetValue(PasswordProperty);
    }

    public static void SetPassword(DependencyObject dp, string value)
    {
        dp.SetValue(PasswordProperty, value);
    }

    private static bool GetIsUpdating(DependencyObject dp)
    {
        return (bool)dp.GetValue(IsUpdatingProperty);
    }

    private static void SetIsUpdating(DependencyObject dp, bool value)
    {
        dp.SetValue(IsUpdatingProperty, value);
    }

    private static void OnPasswordPropertyChanged(DependencyObject sender,
        DependencyPropertyChangedEventArgs e)
    {
        var passwordBox = sender as PasswordBox;
        if (passwordBox != null)
        {
            passwordBox.PasswordChanged -= PasswordChanged;

            if (!GetIsUpdating(passwordBox))
            {
                passwordBox.Password = (string)e.NewValue;
            }
            passwordBox.PasswordChanged += PasswordChanged;
        }
    }

    private static void Attach(DependencyObject sender,
        DependencyPropertyChangedEventArgs e)
    {
        var passwordBox = sender as PasswordBox;

        if (passwordBox == null)
            return;

        if ((bool)e.OldValue)
        {
            passwordBox.PasswordChanged -= PasswordChanged;
        }

        if ((bool)e.NewValue)
        {
            passwordBox.PasswordChanged += PasswordChanged;
        }
    }

    private static void PasswordChanged(object sender, RoutedEventArgs e)
    {
        var passwordBox = sender as PasswordBox;
        SetIsUpdating(passwordBox, true);
        if (passwordBox != null)
        {
            SetPassword(passwordBox, passwordBox.Password);
            SetIsUpdating(passwordBox, false);
        }
    }
}

我在 View 模型中找到的一些代码(非常感谢 Den)。

private static bool KhKeyIntercepted(KeyBoardHook.KeyboardHookEventArgs e)
    {
       if (e.KeyCode < 65 || e.KeyCode > 90) //a - z
        {
            if (e.KeyCode > 47 && e.KeyCode < 57) // 0-9
                return true;

            if (e.KeyCode == 220
                || e.KeyCode == 191
                || e.KeyCode == 189
                || e.KeyCode == 160
                || e.KeyCode == 161
                || e.KeyCode == 8
                || e.KeyCode == 9) //slsah, minus, left and right shift, tab and backspace
                return true;

            return false;
        }
        return true;
    }
4

1 回答 1

0

你在球场的正确部分。它是视图模型中的一个键盘挂钩例程。它忽略了低于 48 的关键代码。我现在已经对其进行了修改(并添加了注释)以忽略低于 47 的代码。ASCII 48 是字符“0”。非常感谢您帮助我到达那里。在你打开灯之前,我一定已经浏览过这段代码几次了。

于 2014-07-30T08:59:56.443 回答