MVVM模式的demo
using System;using System.Windows.Input;namespace MVVMDemo{ public class SampleCommand : ICommand { private readonly Action _execute; private readonly Func<bool> _canExecute; public SampleCommand(Action execute) : this(execute, null) { } public SampleCommand(Action execute, Func<bool> canExecute) { if (execute == null) { throw new ArgumentNullException("execute"); } this._execute = execute; if (canExecute != null) { this._canExecute = canExecute; } } public bool CanExecute(object parameter) { return this._canExecute == null ? true : this._canExecute.Invoke(); } public event EventHandler CanExecuteChanged; public void Execute(object parameter) { if (this.CanExecute(parameter) && this._execute != null) { this._execute.Invoke(); } } }}
评论