我有一个RelayCommand类:
public class RelayCommand : ICommand
{
private readonly Func<bool> canExecuteEvaluator;
private readonly Action methodToExecute;
public RelayCommand(Action methodToExecute, Func<bool> canExecuteEvaluator)
{
this.methodToExecute = methodToExecute;
this.canExecuteEvaluator = canExecuteEvaluator;
}
public RelayCommand(Action methodToExecute)
: this(methodToExecute, null)
{
}
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
public bool CanExecute(object parameter)
{
if (this.canExecuteEvaluator == null)
{
return true;
}
else
{
bool result = this.canExecuteEvaluator.Invoke();
return result;
}
}
public void Execute(object parameter)
{
this.methodToExecute.Invoke();
}
在我的视图模型中,我创建如下命令:
public RelayCommand CmdUpdate { get; private set; }
CmdUpdate = new RelayCommand(() => Updater.CheckForUpdate());
Updater.CheckForUpdate()只是检查是否有可用的更新并显示一个消息框。
然后将命令绑定到我视图中的菜单项。
当我按该项目时,消息框会突然弹出并再次消失。问题在哪里。非常感谢您的解释,所以我可以理解。