Created
March 15, 2013 01:46
-
-
Save JakeGinnivan/5166898 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DelegateCommand : DelegateCommand<object> | |
{ | |
public DelegateCommand(Action executeMethod) | |
: base(o => executeMethod()) | |
{ | |
} | |
public DelegateCommand(Action executeMethod, Func<bool> canExecuteMethod) | |
: base(o => executeMethod(), o => canExecuteMethod()) | |
{ | |
} | |
} | |
/// <summary> | |
/// A command that calls the specified delegate when the command is executed. | |
/// </summary> | |
/// <typeparam name="T"></typeparam> | |
public class DelegateCommand<T> : ICommand, IRaiseCanExecuteChanged | |
{ | |
private readonly Func<T, bool> _canExecuteMethod; | |
private readonly Action<T> _executeMethod; | |
private bool _isExecuting; | |
public DelegateCommand(Action<T> executeMethod) | |
: this(executeMethod, null) | |
{ | |
} | |
public DelegateCommand(Action<T> executeMethod, Func<T, bool> canExecuteMethod) | |
{ | |
if ((executeMethod == null) && (canExecuteMethod == null)) | |
{ | |
throw new ArgumentNullException("executeMethod", @"Execute Method cannot be null"); | |
} | |
_executeMethod = executeMethod; | |
_canExecuteMethod = canExecuteMethod; | |
} | |
public event EventHandler CanExecuteChanged | |
{ | |
add | |
{ | |
CommandManager.RequerySuggested += value; | |
} | |
remove | |
{ | |
CommandManager.RequerySuggested -= value; | |
} | |
} | |
public void RaiseCanExecuteChanged() | |
{ | |
CommandManager.InvalidateRequerySuggested(); | |
} | |
bool ICommand.CanExecute(object parameter) | |
{ | |
return !_isExecuting && CanExecute((T)parameter); | |
} | |
void ICommand.Execute(object parameter) | |
{ | |
_isExecuting = true; | |
try | |
{ | |
RaiseCanExecuteChanged(); | |
Execute((T)parameter); | |
} | |
finally | |
{ | |
_isExecuting = false; | |
RaiseCanExecuteChanged(); | |
} | |
} | |
public bool CanExecute(T parameter) | |
{ | |
if (_canExecuteMethod == null) | |
return true; | |
return _canExecuteMethod(parameter); | |
} | |
public void Execute(T parameter) | |
{ | |
_executeMethod(parameter); | |
} | |
} |
Where can I find IRaiseCanExecuteChanged description?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I can't find CommandManager in my Prism 4.1 & Silverlight 5 Application. what is this? More I'm getting Error 5 Cannot find all types required by the 'async' modifier. Are you targeting the wrong framework version, or missing a reference to an assembly? D:\Projecttrial\Aminations\showHideOne\AsyncSupport\AsyncCommandBase.cs 53 9 showHideOne
Error.
How to resolve it and how I can use it in this scenerio
http://stackoverflow.com/questions/25001089/delegatecommand-async-support-prism-4-1/25001195#25001195