Пример #1
0
        private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            TypeIDValidationRule ev = new TypeIDValidationRule();
            NameValidationRule   nv = new NameValidationRule();

            if (ev.Validate(tbID.Text, null).IsValid == true && nv.Validate(tbName.Text, null).IsValid == true)
            {
                btnOK.IsEnabled = true;
            }
            else
            {
                btnOK.IsEnabled = false;
            }
        }
Пример #2
0
        private bool CheckMsg(MainConfig config, Message msg, out string error)
        {
            try
            {
                var    realMsg  = msg as MessageRegisterRequest;
                UIUser realUser = FindUser(config, realMsg.Account);
                if (null != realUser)
                {
                    throw new Exception("账号已存在");
                }
                var validateResult = new AccountValidationRule().Validate(realMsg.Account, null);
                if (validateResult.IsValid == false)
                {
                    throw new Exception(validateResult.ErrorContent.ToString());
                }

                validateResult = new PasswordValidationRule().Validate(realMsg.Password, null);
                if (validateResult.IsValid == false)
                {
                    throw new Exception(validateResult.ErrorContent.ToString());
                }

                validateResult = new NameValidationRule().Validate(realMsg.Name, null);
                if (validateResult.IsValid == false)
                {
                    throw new Exception(validateResult.ErrorContent.ToString());
                }

                validateResult = new IntroduceValidationRule().Validate(realMsg.Introduce, null);
                if (validateResult.IsValid == false)
                {
                    throw new Exception(validateResult.ErrorContent.ToString());
                }

                error = null;
                return(true);
            }
            catch (Exception ex)
            {
                error = ex.Message;
                return(false);
            }
        }
Пример #3
0
        private void OnApplyRegister(object sender, RoutedEventArgs e)
        {
            try
            {
                //检查注册信息
                if (userHead.Source == null)
                {
                    throw new Exception("请上传头像!");
                }

                var validateResult = new AccountValidationRule().Validate(RegisteredAccount, null);
                if (validateResult.IsValid == false)
                {
                    throw new Exception(validateResult.ErrorContent.ToString());
                }

                if (repeatedRegisteredPasswordBox.Password != registeredPasswordBox.Password)
                {
                    throw new Exception(PasswordValidattion.GetErrorMessage(repeatedRegisteredPasswordBox));
                }

                validateResult = new PasswordValidationRule().Validate(RegisteredPassword, null);
                if (validateResult.IsValid == false)
                {
                    throw new Exception(validateResult.ErrorContent.ToString());
                }

                validateResult = new NameValidationRule().Validate(RegisteredName, null);
                if (validateResult.IsValid == false)
                {
                    throw new Exception(validateResult.ErrorContent.ToString());
                }
                //连接网络
                if (_protocol.IsConnected == false)
                {
                    _protocol.Connect();
                }
                if (_protocol.IsConnected == false)
                {
                    return;
                }
                //填写消息
                var msg = new MessageRegisterRequest()
                {
                    CallID    = DateTime.Now.GetHashCode(),
                    UserID    = Account.GetHashCode(),
                    Account   = RegisteredAccount,
                    Password  = RegisteredPassword,
                    Name      = RegisteredName,
                    Introduce = RegisteredIntroduce,
                    User      = _protocol.UserList.First().Value
                };
                var encoder      = new BmpBitmapEncoder();
                var bitmapSource = this.userHead.Source as BitmapSource;
                encoder.Frames.Add(BitmapFrame.Create(bitmapSource));
                using (var stream = new MemoryStream())
                {
                    encoder.Save(stream);
                    msg.HeadsculptStream = stream.GetBuffer();
                }
                //发送
                string error  = null;
                bool   result = false;
                using (var signal = new ManualResetEventSlim())
                {
                    EventHandler <Message> waitBack = (object sd, Message respondsMsg) =>
                    {
                        if (msg.CallID != respondsMsg.CallID)
                        {
                            return;
                        }
                        if (respondsMsg.MessageID == MessageID.MI_REGISTER_FAILED)
                        {
                            error = (respondsMsg as MessageRegisterFailed).Error;
                            signal.Set();
                        }
                        else if (respondsMsg.MessageID == MessageID.MI_REGISTER_SUCCESSFULLY)
                        {
                            var realMsg = (respondsMsg as MessageRegisterSuccessfully);
                            result = true;
                            signal.Set();
                        }
                    };
                    this._protocol.MessageEvent += waitBack;
                    this._protocol.SendBigMsg(msg);
                    if (signal.Wait(2000) == false)
                    {
                        error = "服务器没有响应,请重试!";
                    }
                    this._protocol.MessageEvent -= waitBack;
                }
                if (result == false)
                {
                    throw new Exception(error);
                }
                MessageWindow.Show(this, "注册成功,赶紧登录去吧!");
                this.OnLoginState();
            }
            catch (Exception ex)
            {
                MessageWindow.Show(this, ex.Message);
            }
            _protocol.Disconnect();
        }