0

我在这里和互联网上查看了如何解决这个问题的多种解决方案,但我似乎无法理解。我只是想为我正在开发的应用程序进行简单的登录。最终我会将它连接到服务器上的 SQL 数据库,但现在忘记了。这是代码:

<Controls:MetroWindow x:Class="ScotiaPlayTrade.Wpf.Application.LoginWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:ff="clr-namespace:ScotiaPlayTrade.Wpf.Application"              
    xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
    xmlns:System="clr-namespace:System;assembly=mscorlib" Title="Authentication"
    Height="400" Width="600" WindowStartupLocation="CenterScreen" TitleForeground="#999988"
    ResizeMode="NoResize" WindowStyle="None" WindowState="Normal" ShowMaxRestoreButton="False">

<Grid>
    <Grid.ColumnDefinitions>
          <ColumnDefinition Width="Auto"/>
          <ColumnDefinition Width="Auto"/>
    </Grid.ColumnDefinitions>

    <Grid.RowDefinitions>
         <RowDefinition Height="Auto"/>
         <RowDefinition Height="Auto"/>
         <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>

        <!--Username Layout-->
    <TextBlock 
         Margin="10,10,10,10" 
        Grid.Column="0" 
        Grid.Row="0">
        User Name
    </TextBlock>
    <TextBox 
         Margin="10,10,10,10" 
        x:Name="userName" 
        Grid.Column="5" 
        Grid.Row="0" 
        Text="{x:Static System:Environment.UserName}"
        IsReadOnly="True">
    </TextBox>

    <!--Password Layout-->
    <TextBlock 
         Margin="10,10,10,10" 
        Grid.Column="0" 
        Grid.Row="1">
        Password
    </TextBlock>

    <PasswordBox
        Margin="10,10,10,10" 
        Width="200" 
        x:Name="PasswordBox" 
        ff:PasswordBoxAssistant.BindPassword="true"  x:PasswordBoxAssistant.BoundPassword="{Binding Path=Password, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
        Grid.Column="1" 
        Grid.Row="1">
    </PasswordBox >

    <!--Buttons Layout-->
    <StackPanel Margin="3,3,3,3" Orientation="Horizontal" Grid.Column="1" Grid.Row="2">
        <Button 
            Margin="10,10,10,10" 
            Click="Login_Click" 
            IsDefault="True">
            Login
        </Button>
        <Button 
            Margin="10,10,10,10" 
            Click="Exit_Click" 
            IsCancel="True">
            Exit
        </Button>

        <Button 
            Margin="10,10,10,10" 
            Click="AddNewUser_Click" 
            IsCancel="True">
            New User
        </Button>

    </StackPanel>
</Grid>

那是我的 LoginWindow.xaml,这是我的窗口 cs 代码。

public partial class LoginWindow
{
    public LoginWindow()
    {
        InitializeComponent();
    }

    //Clicking Login Button
    private void Login_Click(object sender, RoutedEventArgs e)
    {
        //Check that password field is not null
        if (PasswordBox != null)
        {
            //Check that username matches password, if it matches then open main window 

            //Launch the main window after authentication is complete
            MainWindow myMainWindow = new MainWindow();
            myMainWindow.Show();

            //Close the login screen
            Close();
        }
        else
        {
            MessageBox.Show("Password field cannot be empty!");

        }


    }

    //Clicking Exit Button
    private void Exit_Click(object sender, RoutedEventArgs e)
    {
        Close();

    }

    //Clicking Exit Button
    private void AddNewUser_Click(object sender, RoutedEventArgs e)
    {
        MessageBox.Show("Not yet implemented");

    }
}

我已经用密码框尝试了一切,它一直给我一个 WPF 不支持它的错误,比如 PasswordBoxAssistant 和 Helper。将不胜感激任何帮助。以下是错误消息: Windows Presentation Foundation (WPF) 项目不支持错误 1 ​​PasswordBoxAssistant。错误 5 XML 命名空间“ http://schemas.microsoft.com/winfx/2006/xaml
” 中不存在属性“PasswordBoxAssistant.BindPassword” 。第 50 行位置 13。

好的,现在我创建了一个名为 PasswordValidation 的类并添加了 PasswordBoxAssistant 代码。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace ScotiaPlayTrade.Wpf.Application
{
    public static class PasswordBoxAssistant
    {
        public static readonly DependencyProperty BoundPassword =
            DependencyProperty.RegisterAttached("BoundPassword", typeof(string),       typeof(PasswordBoxAssistant), new PropertyMetadata(string.Empty, OnBoundPasswordChanged));

        public static readonly DependencyProperty BindPassword = DependencyProperty.RegisterAttached(
            "BindPassword", typeof(bool), typeof(PasswordBoxAssistant), new PropertyMetadata(false, OnBindPasswordChanged));

        private static readonly DependencyProperty UpdatingPassword =
            DependencyProperty.RegisterAttached("UpdatingPassword", typeof(bool), typeof(PasswordBoxAssistant), new PropertyMetadata(false));

        private static void OnBoundPasswordChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            PasswordBox box = d as PasswordBox;

            // only handle this event when the property is attached to a PasswordBox
            // and when the BindPassword attached property has been set to true
            if (d == null || !GetBindPassword(d))
            {
                return;
            }

            // avoid recursive updating by ignoring the box's changed event
            box.PasswordChanged -= HandlePasswordChanged;

            string newPassword = (string)e.NewValue;

            if (!GetUpdatingPassword(box))
            {
                box.Password = newPassword;
            }

            box.PasswordChanged += HandlePasswordChanged;
        }

        private static void OnBindPasswordChanged(DependencyObject dp, DependencyPropertyChangedEventArgs e)
        {
            // when the BindPassword attached property is set on a PasswordBox,
            // start listening to its PasswordChanged event

            PasswordBox box = dp as PasswordBox;

            if (box == null)
            {
                return;
            }

            bool wasBound = (bool)(e.OldValue);
            bool needToBind = (bool)(e.NewValue);

            if (wasBound)
            {
                box.PasswordChanged -= HandlePasswordChanged;
            }

            if (needToBind)
            {
                box.PasswordChanged += HandlePasswordChanged;
            }
        }

        private static void HandlePasswordChanged(object sender, RoutedEventArgs e)
        {
            PasswordBox box = sender as PasswordBox;

            // set a flag to indicate that we're updating the password
            SetUpdatingPassword(box, true);
            // push the new password into the BoundPassword property
            SetBoundPassword(box, box.Password);
            SetUpdatingPassword(box, false);
        }

        public static void SetBindPassword(DependencyObject dp, bool value)
        {
            dp.SetValue(BindPassword, value);
        }

        public static bool GetBindPassword(DependencyObject dp)
        {
            return (bool)dp.GetValue(BindPassword);
        }

        public static string GetBoundPassword(DependencyObject dp)
        {
            return (string)dp.GetValue(BoundPassword);
        }

        public static void SetBoundPassword(DependencyObject dp, string value)
        {
            dp.SetValue(BoundPassword, value);
        }

        private static bool GetUpdatingPassword(DependencyObject dp)
        {
            return (bool)dp.GetValue(UpdatingPassword);
        }

        private static void SetUpdatingPassword(DependencyObject dp, bool value)
        {
            dp.SetValue(UpdatingPassword, value);
        }
    }

}

以下是我得到的错误: 错误 1 ​​名称“PasswordBoxAssistant”在命名空间“clr-namespace:ScotiaPlayTrade.Wpf.Application”中不存在。50 13
错误 2 Windows Presentation Foundation (WPF) 项目不支持 PasswordBoxAssistant。50 58
错误 3 在类型“PasswordBoxAssistant”中找不到可附加属性“BindPassword”。50 13

4

0 回答 0