(最近的相关问题)
我执行LogInRequest()
which calls LogInView.ShowDialog()
。该视图有一个名为VerifyLogInCommand
. 当命令被执行时,它完成后this.CloseAction()
似乎关闭了对话框。但是,在对话框关闭(不间断)后,我在该视图命令的 CanExecute 方法VerifyLogInCanExecute
中的断点仍然被命中。在调用 ShowDialog 后,我尝试将视图设置为 null,但没有任何变化。
当窗口关闭/为空时,为什么仍在评估 Command/CanExecute?
LogInView.xaml.cs
public LogInOutView()
{
InitializeComponent();
// Data context
IModule existingVM = SessionViewModel.Instance.ModulesOpen.Single(mod => mod.ModuleName == "LogIn");
LogInViewModel livm = (LogInViewModel)existingVM;
this.DataContext = livm;
// Cancel Handler
livm.CloseAction = new Action(() => this.Close());
}
LogInViewModel.cs
public Action CloseAction { get; set; }
private RelayCommand verifyLogInCommand;
public RelayCommand VerifyLogInCommand
{
get
{
if (verifyLogInCommand == null)
{
verifyLogInCommand = new RelayCommand(
param => VerifyLogInExecute(),
param => VerifyLogInCanExecute);
}
return verifyLogInCommand;
}
}
public void VerifyLogInExecute()
{
// Validate Login
Employee employee = ValidateLogin(Password);
// Clear password field
ResetExecute();
// Return false if invalid login
if (employee == null)
{
Result = LogInOutDialogResults.Cancel;
ConfirmationView c = new ConfirmationView("Invalid Login!");
c.ShowDialog();
return;
}
// Set Result to LogIn status
Result = LogInOutDialogResults.LogIn;
// Set LastAuthorizedEmployee
SessionViewModel.Instance.LastAuthorizedEmployee = employee;
// Close View to go back where it was called
this.CloseAction();
}
public bool VerifyLogInCanExecute
{
get
{
// Password length restrictions
if (!CheckRequiredPasswordLength(Password)) { return false; }
return true;
}
}
public static LogInOutDialogResults LogInRequest()
{
// Show Login View
LogInOutDialogResults LogInOutResult = LogInOutDialogResults.Cancel;
LogInOutView LogInOutView = new LogInOutView()
{
Title = "Log In",
ShowInTaskbar = false,
Topmost = true,
ResizeMode = ResizeMode.NoResize,
Owner = SessionViewModel.Instance.ProfitPOSView
};
LogInOutView.ShowDialog();
LogInOutResult = ((LogInViewModel)LogInOutView.DataContext).Result;
// LogIn
if (LogInOutResult == LogInOutDialogResults.LogIn)
{
LogInOutView = null;
return LogInOutDialogResults.LogIn;
}
}