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などがあります。助けていただければ幸いです。エラー メッセージは次のとおりです。 エラー 1 PasswordBoxAssistant は Windows Presentation Foundation (WPF) プロジェクトではサポートされていません。エラー 5 プロパティ 'PasswordBoxAssistant.BindPassword' は、XML 名前空間 ' http://schemas.microsoft.com/winfx/2006/xaml
' に存在しません。行 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 PasswordBoxAssistant は Windows Presentation Foundation (WPF) プロジェクトではサポートされていません。50 58
エラー 3 添付可能なプロパティ 'BindPassword' がタイプ 'PasswordBoxAssistant' で見つかりませんでした。50 13

4

0 に答える 0