mirror of
https://github.com/VitalickS/BrightSharp.Toolkit.git
synced 2026-03-21 02:21:15 +00:00
.net copy
This commit is contained in:
74
BrightSharp.NET/Commands/AsyncCommand.cs
Normal file
74
BrightSharp.NET/Commands/AsyncCommand.cs
Normal file
@@ -0,0 +1,74 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Input;
|
||||
|
||||
namespace BrightSharp.Commands
|
||||
{
|
||||
public class AsyncCommand : ICommand
|
||||
{
|
||||
private readonly Func<object, Task> _execute;
|
||||
private readonly Func<object, bool> _canExecute;
|
||||
private Action _onComplete;
|
||||
private Action<Exception> _onFail;
|
||||
private bool _isExecuting;
|
||||
|
||||
public AsyncCommand(Func<Task> execute) : this(p => execute?.Invoke())
|
||||
{
|
||||
}
|
||||
|
||||
public AsyncCommand(Func<object, Task> execute) : this(execute, o => true)
|
||||
{
|
||||
}
|
||||
|
||||
public AsyncCommand(Func<object, Task> execute, Func<object, bool> canExecute)
|
||||
{
|
||||
_execute = execute;
|
||||
_canExecute = canExecute;
|
||||
}
|
||||
|
||||
public AsyncCommand OnComplete(Action onComplete)
|
||||
{
|
||||
_onComplete = onComplete;
|
||||
return this;
|
||||
}
|
||||
public AsyncCommand OnFail(Action<Exception> onFail)
|
||||
{
|
||||
_onFail = onFail;
|
||||
return this;
|
||||
}
|
||||
|
||||
public bool CanExecute(object parameter)
|
||||
{
|
||||
return !_isExecuting && _canExecute(parameter);
|
||||
}
|
||||
|
||||
public event EventHandler CanExecuteChanged;
|
||||
|
||||
public async void Execute(object parameter)
|
||||
{
|
||||
_isExecuting = true;
|
||||
OnCanExecuteChanged();
|
||||
try
|
||||
{
|
||||
await _execute(parameter);
|
||||
_onComplete?.Invoke();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
while (ex.InnerException != null)
|
||||
ex = ex.InnerException;
|
||||
_onFail?.Invoke(ex);
|
||||
}
|
||||
finally
|
||||
{
|
||||
_isExecuting = false;
|
||||
OnCanExecuteChanged();
|
||||
}
|
||||
}
|
||||
|
||||
public virtual void OnCanExecuteChanged()
|
||||
{
|
||||
CanExecuteChanged?.Invoke(this, new EventArgs());
|
||||
}
|
||||
}
|
||||
}
|
||||
71
BrightSharp.NET/Commands/RelayCommand.cs
Normal file
71
BrightSharp.NET/Commands/RelayCommand.cs
Normal file
@@ -0,0 +1,71 @@
|
||||
using System;
|
||||
using System.Windows.Input;
|
||||
|
||||
namespace BrightSharp.Commands
|
||||
{
|
||||
public class RelayCommand : ICommand
|
||||
{
|
||||
private readonly Action _methodToExecute;
|
||||
private readonly Action<object> _methodToExecuteWithParam;
|
||||
private readonly Func<object, bool> _canExecuteEvaluator;
|
||||
|
||||
public event EventHandler CanExecuteChanged
|
||||
{
|
||||
add { CommandManager.RequerySuggested += value; }
|
||||
remove { CommandManager.RequerySuggested -= value; }
|
||||
}
|
||||
|
||||
public RelayCommand(Action<object> methodToExecute, Func<object, bool> canExecuteEvaluator = null)
|
||||
{
|
||||
_methodToExecuteWithParam = methodToExecute;
|
||||
_canExecuteEvaluator = canExecuteEvaluator;
|
||||
}
|
||||
|
||||
public RelayCommand(Action methodToExecute)
|
||||
{
|
||||
_methodToExecute = methodToExecute;
|
||||
}
|
||||
|
||||
public bool CanExecute(object parameter)
|
||||
{
|
||||
if (_canExecuteEvaluator == null)
|
||||
return true;
|
||||
else
|
||||
return _canExecuteEvaluator.Invoke(parameter);
|
||||
}
|
||||
public void Execute(object parameter)
|
||||
{
|
||||
_methodToExecuteWithParam?.Invoke(parameter);
|
||||
_methodToExecute?.Invoke();
|
||||
}
|
||||
}
|
||||
|
||||
public class RelayCommand<T> : ICommand where T : class
|
||||
{
|
||||
private readonly Action<T> _methodToExecuteWithParam;
|
||||
private readonly Func<T, bool> _canExecuteEvaluator;
|
||||
|
||||
public event EventHandler CanExecuteChanged
|
||||
{
|
||||
add { CommandManager.RequerySuggested += value; }
|
||||
remove { CommandManager.RequerySuggested -= value; }
|
||||
}
|
||||
|
||||
public RelayCommand(Action<T> methodToExecute, Func<T, bool> canExecuteEvaluator = null)
|
||||
{
|
||||
_methodToExecuteWithParam = methodToExecute;
|
||||
_canExecuteEvaluator = canExecuteEvaluator;
|
||||
}
|
||||
|
||||
public bool CanExecute(object parameter)
|
||||
{
|
||||
if (_canExecuteEvaluator == null)
|
||||
return true;
|
||||
return _canExecuteEvaluator.Invoke(parameter as T);
|
||||
}
|
||||
public void Execute(object parameter)
|
||||
{
|
||||
_methodToExecuteWithParam?.Invoke(parameter as T);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user