有 2 个全局变量。controlDisable 变量绑定到窗口元素的 IsEnabled 属性。:
private bool completedLogin = true;
public bool CompletedLogin
{
get
{
return completedLogin;
}
set
{
completedLogin = value;
NotifyPropertyChanged();
}
}
private bool controlDisable = true;
public bool ControlDisable
{
get
{
return controlDisable;
}
set
{
controlDisable = value;
NotifyPropertyChanged();
}
}
有一个方法是在另一个线程中单击按钮时执行的(使用的是 BackgroundWorker 类):
private RelayCommand loginCommand;
public RelayCommand LoginCommand
{
get
{
return loginCommand ??
(loginCommand = new RelayCommand(obj =>
{
ControlDisable = false;
CompletedLogin = false;
Password = GetPassword(obj);
worker = new BackgroundWorker();
worker.DoWork += (obj, ea) => LoginUser();
worker.RunWorkerAsync();
worker.RunWorkerCompleted += (obj, ea) => OpenGeneral();
}));
}
}
private async void LoginUser()
{
if (Login != "" && Password != "")
{
using (MSGCoreContext db = new MSGCoreContext())
{
user = await db.Users.FirstOrDefaultAsync(u => u.Login == Login);
if (user != null)
{
userId = user.UserId;
userRole = user.Role;
if (PBKDF2HashHelper.VerifyPassword(Password, user.Password))
{
CompletedLogin = true;
}
else { Warning = "Неверный логин или пароль!"; CompletedLogin = false; ControlDisable = true; }
}
else { Warning = "Пользователь не существует!"; CompletedLogin = false; ControlDisable = true; }
}
}
else
{
Warning = "Заполните все поля!";
CompletedLogin = false;
ControlDisable = true;
}
}
在 LoginUser 方法完成执行后,会触发 RunWorkerCompleted 事件,该事件会调用 OpenGeneral 方法:
private void OpenGeneral()
{
if (CompletedLogin)
{
var displayRootRegistry = (Application.Current as App).displayRootRegistry;
var dialogWindowViewModel = new GeneralViewModel(Login, userRole);
displayRootRegistry.ShowPresentation(dialogWindowViewModel);
Application.Current.MainWindow.Close();
}
}
ControlDisable 值已更改,窗口中的元素设置为 IsEnabled = false 状态。而且 OpenGeneral 方法中 CompletedLogin 变量的值总是假的,我怎样才能让 CompletedLogin 的值也改变呢?