我正在实现一个带有执行和 canExecute 部分的 RelayCommand。RelayCommand 在没有 canExecute 部分时工作,但是当我添加 canExecute 部分时,命令会锁定按钮。RelayCommand 只检查按钮是否可以执行,只要 CanExecute 部分为真。一旦 canExecute 部分变为假,按钮就不能再被点击,即使它应该被点击。如何确保每次单击按钮时它都会控制是否可以执行,并且一旦无法执行就不会永远锁定它?
RedoCommand = new RelayCommand(undoRedoController.Redo,undoRedoController.CanRedo);
public bool CanRedo()
{
redoStack.Count();
redoStack.Any();
return redoStack.Any();
}
public void Redo()
{
if (redoStack.Count() <= 0) throw new InvalidOperationException();
IUndoRedoCommand command = redoStack.Pop();
undoStack.Push(command);
command.Execute();
}
public class UndoRedoController
{
private static UndoRedoController controller = new UndoRedoController();
private readonly Stack<IUndoRedoCommand> undoStack = new Stack<IUndoRedoCommand>();
private readonly Stack<IUndoRedoCommand> redoStack = new Stack<IUndoRedoCommand>();
private UndoRedoController() { }
public static UndoRedoController GetInstance() { return controller; }