Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
201505-MVA/SplitView/SplitView/SplitView-Template10/Mvvm/Command.cs
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
81 lines (70 sloc)
2.17 KB
This file contains 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
using System; | |
namespace Template10.Mvvm | |
{ | |
// http://codepaste.net/jgxazh | |
using System.Diagnostics; | |
public class Command : System.Windows.Input.ICommand | |
{ | |
private readonly Action _execute; | |
private readonly Func<bool> _canExecute; | |
public event EventHandler CanExecuteChanged; | |
public Command(Action execute, Func<bool> canexecute = null) | |
{ | |
if (execute == null) | |
throw new ArgumentNullException("execute"); | |
_execute = execute; | |
_canExecute = canexecute ?? (() => true); | |
} | |
[DebuggerStepThrough] | |
public bool CanExecute(object p) | |
{ | |
try { return _canExecute(); } | |
catch { return false; } | |
} | |
public void Execute(object p) | |
{ | |
if (!CanExecute(p)) | |
return; | |
try { _execute(); } | |
catch { Debugger.Break(); } | |
} | |
public void RaiseCanExecuteChanged() | |
{ | |
CanExecuteChanged?.Invoke(this, EventArgs.Empty); | |
} | |
} | |
public class Command<T> : System.Windows.Input.ICommand | |
{ | |
private readonly Action<T> _execute; | |
private readonly Func<T, bool> _canExecute; | |
public event EventHandler CanExecuteChanged; | |
public Command(Action<T> execute, Func<T, bool> canexecute = null) | |
{ | |
if (execute == null) | |
throw new ArgumentNullException("execute"); | |
_execute = execute; | |
_canExecute = canexecute ?? (e => true); | |
} | |
[DebuggerStepThrough] | |
public bool CanExecute(object p) | |
{ | |
try | |
{ | |
var _Value = (T)Convert.ChangeType(p, typeof(T)); | |
return _canExecute == null ? true : _canExecute(_Value); | |
} | |
catch { return false; } | |
} | |
public void Execute(object p) | |
{ | |
if (!CanExecute(p)) | |
return; | |
var _Value = (T)Convert.ChangeType(p, typeof(T)); | |
_execute(_Value); | |
} | |
public void RaiseCanExecuteChanged() | |
{ | |
CanExecuteChanged?.Invoke(this, EventArgs.Empty); | |
} | |
} | |
} |