using System; using System.Windows; using System.Windows.Input; using Caliburn.Messaging; using Caliburn.Metadata; using Caliburn.Services; namespace Caliburn.Actions { public class ActionCommand : ICommand { #region Fields private readonly ActionInfo _actionInfo; private readonly Func _canExecute; private readonly object _presenter; #endregion #region Constructor public ActionCommand(object presenter, string actionName, Func canExecute) { _presenter = presenter; _canExecute = canExecute; PresenterInfo presenterInfo = DI.Resolve().GetInfo(_presenter.GetType()); _actionInfo = presenterInfo.FindAction(actionName); } public ActionCommand(object presenter, Action action, Func canExecute) : this(presenter, action.Method.Name, canExecute) { } #endregion #region ICommand Members public event EventHandler CanExecuteChanged { add { CommandManager.RequerySuggested += value; } remove { CommandManager.RequerySuggested -= value; } } public void Execute(object parameter) { ActionMessage message = new ActionMessage(); Parameter p = new Parameter(); p.Value = parameter; message.Parameters.Add(p); _actionInfo.Execute(_presenter, message, parameter as DependencyObject); } public bool CanExecute(object parameter) { if(_canExecute != null) return _canExecute(parameter); else return true; } #endregion } }