Beispiel #1
0
        private void PasswordBox_PasswordChanged(object sender, RoutedEventArgs e)
        {
            PasswordBox textBox = sender as PasswordBox;

            if (String.IsNullOrEmpty(textBox.Password))
            {
                textBox.GetBindingExpression(TextBox.TextProperty);
                ValidationError validationError = new ValidationError(new NotEmptyValidationRule(), textBox.GetBindingExpression(PasswordBox.DataContextProperty));
                validationError.ErrorContent = "Field is required.";

                Validation.MarkInvalid(
                    textBox.GetBindingExpression(PasswordBox.DataContextProperty),
                    validationError);
            }
            else if (textBox.Password.Length < 8)
            {
                textBox.GetBindingExpression(TextBox.TextProperty);
                ValidationError validationError = new ValidationError(new NotEmptyValidationRule(), textBox.GetBindingExpression(PasswordBox.DataContextProperty));
                validationError.ErrorContent = "At least 8 characters.";

                Validation.MarkInvalid(
                    textBox.GetBindingExpression(PasswordBox.DataContextProperty),
                    validationError);
            }
            else
            {
                Validation.ClearInvalid(textBox.GetBindingExpression(PasswordBox.DataContextProperty));
            }
        }
Beispiel #2
0
        public void BeginLogin()
        {
#if WPF
            //not needed, handled in XAML
            if (PasswordField != null)
            {
                Password = PasswordField.Password;
            }
#else
            // Needed so if ENTER was pressed in one of the fields, it's binding will be updated.
            BindingExpression userNamedBindingExpression = UserNameField.GetBindingExpression(TextBox.TextProperty);
            userNamedBindingExpression.UpdateSource();
            BindingExpression passwordBindingExpression = PasswordField.GetBindingExpression(PasswordBox.PasswordProperty);
            passwordBindingExpression.UpdateSource();
#endif

            BusyStatusText = "Logging in...";
            IsBusy         = true;
            _canLogin      = false;

            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs("Login"));
            }

            if (LoginButton != null)
            {
                LoginButton.IsEnabled = false;
            }

            _context.CreateSession(UserName, Password, _context.PreferredSite, "", AuthenticationMode, AppContext_AuthenticationCompleted);
        }
Beispiel #3
0
        private void passwordBox_PasswordChanged(object sender, TextChangedEventArgs e)
        {
            PasswordBox       passwordBox = sender as PasswordBox;
            BindingExpression bindingExpr = passwordBox.GetBindingExpression(TextBox.TextProperty);

            bindingExpr.UpdateSource();
        }
Beispiel #4
0
        private void LoginBTN(object sender, RoutedEventArgs e)
        {
            bool EverythingFine = true;

            if (String.IsNullOrEmpty(UsernameBox.Text))
            {
                UsernameBox.GetBindingExpression(TextBox.TextProperty);
                ValidationError validationError = new ValidationError(new NotEmptyValidationRule(), UsernameBox.GetBindingExpression(TextBox.TextProperty));
                validationError.ErrorContent = "Field is required.";

                Validation.MarkInvalid(
                    UsernameBox.GetBindingExpression(TextBox.TextProperty),
                    validationError);
                EverythingFine = false;
            }
            else
            {
                Validation.ClearInvalid(UsernameBox.GetBindingExpression(TextBox.TextProperty));
            }
            if (String.IsNullOrEmpty(PasswordBox.Password))
            {
                PasswordBox.GetBindingExpression(TextBox.TextProperty);
                ValidationError validationError = new ValidationError(new NotEmptyValidationRule(), PasswordBox.GetBindingExpression(PasswordBox.DataContextProperty));
                validationError.ErrorContent = "Field is required.";

                Validation.MarkInvalid(
                    PasswordBox.GetBindingExpression(PasswordBox.DataContextProperty),
                    validationError);
                EverythingFine = false;
            }
            else if (PasswordBox.Password.Length < 8)
            {
                PasswordBox.GetBindingExpression(TextBox.TextProperty);
                ValidationError validationError = new ValidationError(new NotEmptyValidationRule(), PasswordBox.GetBindingExpression(PasswordBox.DataContextProperty));
                validationError.ErrorContent = "At least 8 characters.";

                Validation.MarkInvalid(
                    PasswordBox.GetBindingExpression(PasswordBox.DataContextProperty),
                    validationError);
                EverythingFine = false;
            }
            else
            {
                Validation.ClearInvalid(PasswordBox.GetBindingExpression(PasswordBox.DataContextProperty));
            }
            if (EverythingFine)
            {
                //TODO: Sign In Using Communicator
            }
        }
        private static void OnIsDirtyEnabledChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs args)
        {
            PasswordBox pb = (PasswordBox)dependencyObject;

            if (pb != null)
            {
                pb.LostFocus += delegate
                {
                    if (pb.GetBindingExpression(PasswordBox.CacheModeProperty) != null)
                    {
                        //Validation.ClearInvalid(pb.GetBindingExpression(PasswordBox.CacheModeProperty));
                        pb.GetBindingExpression(PasswordBox.CacheModeProperty).UpdateTarget();
                    }

                    if (!(bool)pb.GetValue(ShowErrorTemplateProperty))
                    {
                        pb.SetValue(ShowErrorTemplateProperty, true);
                    }
                }
            }
            ;
        }
    }
Beispiel #6
0
        static void OnPasswordChanged(object sender, RoutedEventArgs e)
        {
            PasswordBox passwordBox = sender as PasswordBox;

            if (passwordBox == null)
            {
                return;
            }

            BindingExpression bindingExpression = passwordBox.GetBindingExpression(PasswordBox.PasswordProperty);

            if (bindingExpression != null)
            {
                bindingExpression.UpdateSource();
            }
        }
        public static void BindFocusedTextBox()
        {
            TextBox     focusTextBox = FocusManager.GetFocusedElement() as TextBox;
            PasswordBox pwdBox       = FocusManager.GetFocusedElement() as PasswordBox;

            if (pwdBox != null)
            {
                var binding = pwdBox.GetBindingExpression(PasswordBox.PasswordProperty);
                if (binding != null)
                {
                    binding.UpdateSource();
                }
            }
            if (focusTextBox != null)
            {
                var binding = focusTextBox.GetBindingExpression(TextBox.TextProperty);
                if (binding != null)
                {
                    binding.UpdateSource();
                }
            }
        }
Beispiel #8
0
        private void LoginBTN_Clicked(object sender, RoutedEventArgs e)
        {
            Button btn = sender as Button;

            MaterialDesignThemes.Wpf.ButtonProgressAssist.SetIsIndeterminate(btn, true);
            MaterialDesignThemes.Wpf.ButtonProgressAssist.SetValue(btn, -1);
            MaterialDesignThemes.Wpf.ButtonProgressAssist.SetIsIndicatorVisible(btn, true);
            btn.IsEnabled       = false;
            SignupBTN.IsEnabled = false;
            bool EverythingFine = true;

            if (String.IsNullOrEmpty(UsernameBox.Text))
            {
                UsernameBox.GetBindingExpression(TextBox.TextProperty);
                ValidationError validationError = new ValidationError(new NotEmptyValidationRule(), UsernameBox.GetBindingExpression(TextBox.TextProperty));
                validationError.ErrorContent = "Field is required.";

                Validation.MarkInvalid(
                    UsernameBox.GetBindingExpression(TextBox.TextProperty),
                    validationError);
                EverythingFine = false;
            }
            else
            {
                Validation.ClearInvalid(UsernameBox.GetBindingExpression(TextBox.TextProperty));
            }
            if (String.IsNullOrEmpty(PasswordBox.Password))
            {
                PasswordBox.GetBindingExpression(TextBox.TextProperty);
                ValidationError validationError = new ValidationError(new NotEmptyValidationRule(), PasswordBox.GetBindingExpression(PasswordBox.DataContextProperty));
                validationError.ErrorContent = "Field is required.";

                Validation.MarkInvalid(
                    PasswordBox.GetBindingExpression(PasswordBox.DataContextProperty),
                    validationError);
                EverythingFine = false;
            }
            else if (PasswordBox.Password.Length < 1)
            {
                PasswordBox.GetBindingExpression(TextBox.TextProperty);
                ValidationError validationError = new ValidationError(new NotEmptyValidationRule(), PasswordBox.GetBindingExpression(PasswordBox.DataContextProperty));
                validationError.ErrorContent = "At least 8 characters.";

                Validation.MarkInvalid(
                    PasswordBox.GetBindingExpression(PasswordBox.DataContextProperty),
                    validationError);
                EverythingFine = false;
            }
            else
            {
                Validation.ClearInvalid(PasswordBox.GetBindingExpression(PasswordBox.DataContextProperty));
            }
            if (EverythingFine)
            {
                LoginRequest loginRequest = new LoginRequest();
                loginRequest.username = UsernameBox.Text;
                loginRequest.password = PasswordBox.Password;

                app.communicator.SocketSendReceive(JsonSerializer.serializeRequest(loginRequest, Constants.LOGIN_REQUEST_CODE)).ContinueWith(task =>
                {
                    ResponseInfo response       = task.Result;
                    LoginResponse loginResponse = JsonDeserializer.deserializeResponse <LoginResponse>(response.buffer);
                    switch (loginResponse.status)
                    {
                    case Constants.LOGIN_SUCCESS:
                        MyMessageQueue.Enqueue("Sign in Successfully!");
                        this.Dispatcher.Invoke(() =>
                        {
                            app.username         = UsernameBox.Text;
                            NavigationService ns = NavigationService.GetNavigationService(this);
                            ns.Navigate(new Uri("Menu.xaml", UriKind.Relative));
                        });
                        break;

                    case Constants.LOGIN_INCORRECT_PASSWORD:
                        MyMessageQueue.Enqueue("Incorrect password.");
                        break;

                    case Constants.LOGIN_USERNAME_NOT_EXIST:
                        MyMessageQueue.Enqueue("Username not exist.");
                        break;

                    case Constants.LOGIN_UNEXPECTED_ERR:
                        MyMessageQueue.Enqueue("There was an unexpected error.");
                        break;

                    case Constants.LOGIN_ALREADY_ONLINE:
                        MyMessageQueue.Enqueue("This Username is already online.");
                        break;
                    }
                    this.Dispatcher.Invoke(() =>
                    {
                        ButtonProgressAssist.SetIsIndeterminate(btn, false);
                        ButtonProgressAssist.SetIsIndicatorVisible(btn, false);
                        btn.IsEnabled       = true;
                        SignupBTN.IsEnabled = true;
                    });
                });
            }
            else
            {
                MaterialDesignThemes.Wpf.ButtonProgressAssist.SetIsIndeterminate(btn, false);
                MaterialDesignThemes.Wpf.ButtonProgressAssist.SetIsIndicatorVisible(btn, false);
                btn.IsEnabled       = true;
                SignupBTN.IsEnabled = true;
            }
        }
Beispiel #9
0
        public object ProvideValue(IServiceProvider serviceProvider)
        {
            var target =
                (IProvideValueTarget)serviceProvider.GetService(typeof(IProvideValueTarget));

            //没有source,默认为数据上下文
            if (Source == null && target is FrameworkElement)
            {
                Source = (target as FrameworkElement).DataContext;
            }
            Binding b = new Binding();

            b.Source = Source;
            if (Path != null)
            {
                b.Path = new PropertyPath(Path);
            }
            //模式默认为双向
            b.Mode = Mode;
            //默认转换器为空串转空值转换器
            b.Converter                   = new EmptyStringConverter();
            b.ConverterParameter          = filter;
            b.ValidatesOnExceptions       = true;
            b.ValidatesOnNotifyDataErrors = true;
            b.ValidatesOnDataErrors       = true;
            b.NotifyOnValidationError     = true;

            //如果目标对象是TextBox,且设置了,当TextBox的Text发生改变时,触发绑定
            if ((target.TargetObject is TextBox) && change)
            {
                TextBox txt = target.TargetObject as TextBox;
                txt.TextChanged += (o, e) =>
                {
                    TextBox text = o as TextBox;
                    var     bindingExpression = text.GetBindingExpression(TextBox.TextProperty);
                    if (filter != null && text.Text != null)
                    {
                        string result = (string)text.Text;
                        //用每一个过滤字符替换value中的内容为空串
                        foreach (char ch in filter)
                        {
                            result = result.Replace("" + ch, "");
                        }
                        text.Text = result;
                    }
                    if (bindingExpression != null)
                    {
                        bindingExpression.UpdateSource();
                    }
                };
            }
            //如果目标对象是PasswordBox,且设置了,当PasswordBox的Password发生改变时,触发绑定
            if ((target.TargetObject is PasswordBox) && change)
            {
                PasswordBox txt = target.TargetObject as PasswordBox;
                txt.PasswordChanged += (o, e) =>
                {
                    PasswordBox text = o as PasswordBox;
                    var         bindingExpression = text.GetBindingExpression(PasswordBox.PasswordProperty);
                    if (filter != null && text.Password != null)
                    {
                        string result = (string)text.Password;
                        //用每一个过滤字符替换value中的内容为空串
                        foreach (char ch in filter)
                        {
                            result = result.Replace("" + ch, "");
                        }
                        text.Password = result;
                    }
                    if (bindingExpression != null)
                    {
                        bindingExpression.UpdateSource();
                    }
                };
            }

            //注册错误通知事件,绑定发生错误(例如类型错误),通知对象
            FrameworkElement fe = (FrameworkElement)target.TargetObject;

            fe.BindingValidationError += (o, e) =>
            {
                if (e.Action.Equals(System.Windows.Controls.ValidationErrorEventAction.Added))
                {
                    CustomTypeHelper ct = (CustomTypeHelper)fe.DataContext;
                    if (ct != null)
                    {
                        ct.OnError(this.Path, e.Error.ErrorContent.ToString());
                    }
                }
            };
            return(b);
        }
Beispiel #10
0
        private void SignupBTN(object sender, RoutedEventArgs e)
        {
            bool EverythingFine = true;

            if (String.IsNullOrEmpty(EmailBox.Text))
            {
                EmailBox.GetBindingExpression(TextBox.TextProperty);
                ValidationError validationError = new ValidationError(new NotEmptyValidationRule(), EmailBox.GetBindingExpression(TextBox.TextProperty));
                validationError.ErrorContent = "Field is required.";

                Validation.MarkInvalid(
                    EmailBox.GetBindingExpression(TextBox.TextProperty),
                    validationError);
                EverythingFine = false;
            }
            else if (!IsValidEmail(EmailBox.Text))
            {
                EmailBox.GetBindingExpression(TextBox.TextProperty);
                ValidationError validationError = new ValidationError(new NotEmptyValidationRule(), EmailBox.GetBindingExpression(TextBox.TextProperty));
                validationError.ErrorContent = "Must be a valid email.";

                Validation.MarkInvalid(
                    EmailBox.GetBindingExpression(TextBox.TextProperty),
                    validationError);
                EverythingFine = false;
            }
            else
            {
                Validation.ClearInvalid(UsernameBox.GetBindingExpression(TextBox.TextProperty));
            }
            if (String.IsNullOrEmpty(UsernameBox.Text))
            {
                UsernameBox.GetBindingExpression(TextBox.TextProperty);
                ValidationError validationError = new ValidationError(new NotEmptyValidationRule(), UsernameBox.GetBindingExpression(TextBox.TextProperty));
                validationError.ErrorContent = "Field is required.";

                Validation.MarkInvalid(
                    UsernameBox.GetBindingExpression(TextBox.TextProperty),
                    validationError);
                EverythingFine = false;
            }
            else
            {
                Validation.ClearInvalid(UsernameBox.GetBindingExpression(TextBox.TextProperty));
            }
            if (String.IsNullOrEmpty(PasswordBox.Password))
            {
                PasswordBox.GetBindingExpression(TextBox.TextProperty);
                ValidationError validationError = new ValidationError(new NotEmptyValidationRule(), PasswordBox.GetBindingExpression(PasswordBox.DataContextProperty));
                validationError.ErrorContent = "Field is required.";

                Validation.MarkInvalid(
                    PasswordBox.GetBindingExpression(PasswordBox.DataContextProperty),
                    validationError);
                EverythingFine = false;
            }
            else if (PasswordBox.Password.Length < 8)
            {
                PasswordBox.GetBindingExpression(TextBox.TextProperty);
                ValidationError validationError = new ValidationError(new NotEmptyValidationRule(), PasswordBox.GetBindingExpression(PasswordBox.DataContextProperty));
                validationError.ErrorContent = "At least 8 characters.";

                Validation.MarkInvalid(
                    PasswordBox.GetBindingExpression(PasswordBox.DataContextProperty),
                    validationError);
                EverythingFine = false;
            }
            else
            {
                Validation.ClearInvalid(PasswordBox.GetBindingExpression(PasswordBox.DataContextProperty));
            }
            if (EverythingFine)
            {
                SignupRequest signupRequest = new SignupRequest();
                signupRequest.username = UsernameBox.Text;
                signupRequest.password = PasswordBox.Password;
                signupRequest.email    = EmailBox.Text;

                app.communicator.SocketSendReceive(JsonSerializer.serializeRequest(signupRequest, Constants.SIGNUP_REQUEST_CODE)).ContinueWith((task) =>
                {
                    ResponseInfo response         = task.Result;
                    SignupResponse signupResponse = JsonDeserializer.deserializeResponse <SignupResponse>(response.buffer);
                    switch (signupResponse.status)
                    {
                    case Constants.SIGNUP_SUCCESS:
                        MyMessageQueue.Enqueue("Sign up Successfully!");
                        this.Dispatcher.Invoke(() =>
                        {
                            app.username         = UsernameBox.Text;
                            NavigationService ns = NavigationService.GetNavigationService(this);
                            ns.Navigate(new Uri("Menu.xaml", UriKind.Relative));
                        });
                        break;

                    case Constants.SIGNUP_EMAIL_OR_USERNAME_EXIST:
                        MyMessageQueue.Enqueue("Username or email already exist.");
                        break;

                    case Constants.SIGNUP_UNEXPECTED_ERR:
                        MyMessageQueue.Enqueue("There was an unexpected error.");
                        break;
                    }
                });
            }
        }