first edit
This commit is contained in:
112
XLIMS.MVVM/Base/AsyncRelayCommand.cs
Normal file
112
XLIMS.MVVM/Base/AsyncRelayCommand.cs
Normal file
@@ -0,0 +1,112 @@
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Input;
|
||||
|
||||
namespace XLIMS.MVVM.Base
|
||||
{
|
||||
/// <summary>
|
||||
/// Асинхронная команда без параметра.
|
||||
/// </summary>
|
||||
public class AsyncRelayCommand : ICommand
|
||||
{
|
||||
private readonly Func<Task> _execute;
|
||||
private readonly Func<bool>? _canExecute;
|
||||
|
||||
private bool _isExecuting;
|
||||
|
||||
public AsyncRelayCommand(Func<Task> execute, Func<bool>? canExecute = null)
|
||||
{
|
||||
_execute = execute ?? throw new ArgumentNullException(nameof(execute));
|
||||
_canExecute = canExecute;
|
||||
}
|
||||
|
||||
public bool CanExecute(object? parameter)
|
||||
{
|
||||
if (_isExecuting) return false;
|
||||
return _canExecute == null || _canExecute();
|
||||
}
|
||||
|
||||
public async void Execute(object? parameter)
|
||||
{
|
||||
if (!CanExecute(parameter)) return;
|
||||
|
||||
try
|
||||
{
|
||||
_isExecuting = true;
|
||||
CommandManager.InvalidateRequerySuggested();
|
||||
|
||||
await _execute();
|
||||
}
|
||||
finally
|
||||
{
|
||||
_isExecuting = false;
|
||||
CommandManager.InvalidateRequerySuggested();
|
||||
}
|
||||
}
|
||||
|
||||
public event EventHandler? CanExecuteChanged
|
||||
{
|
||||
add { CommandManager.RequerySuggested += value; }
|
||||
remove { CommandManager.RequerySuggested -= value; }
|
||||
}
|
||||
|
||||
// Удобный статический метод для создания из метода с параметром (если вдруг понадобится)
|
||||
public static AsyncRelayCommand FromAsync(Func<object?, Task> execute, Func<object?, bool>? canExecute = null)
|
||||
{
|
||||
return new AsyncRelayCommand(() => execute(null), canExecute == null ? null : () => canExecute(null));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Асинхронная команда с параметром (типизированная).
|
||||
/// </summary>
|
||||
public class AsyncRelayCommand<T> : ICommand
|
||||
{
|
||||
private readonly Func<T?, Task> _execute;
|
||||
private readonly Predicate<T?>? _canExecute;
|
||||
|
||||
private bool _isExecuting;
|
||||
|
||||
public AsyncRelayCommand(Func<T?, Task> execute, Predicate<T?>? canExecute = null)
|
||||
{
|
||||
_execute = execute ?? throw new ArgumentNullException(nameof(execute));
|
||||
_canExecute = canExecute;
|
||||
}
|
||||
|
||||
public bool CanExecute(object? parameter)
|
||||
{
|
||||
if (_isExecuting) return false;
|
||||
if (_canExecute == null) return true;
|
||||
|
||||
// Если параметр неправильного типа — считаем, что нельзя выполнить
|
||||
return parameter is T typedParam && _canExecute(typedParam);
|
||||
}
|
||||
|
||||
public async void Execute(object? parameter)
|
||||
{
|
||||
if (!CanExecute(parameter)) return;
|
||||
|
||||
T? typedParam = parameter is T p ? p : default;
|
||||
|
||||
try
|
||||
{
|
||||
_isExecuting = true;
|
||||
CommandManager.InvalidateRequerySuggested();
|
||||
|
||||
await _execute(typedParam);
|
||||
}
|
||||
finally
|
||||
{
|
||||
_isExecuting = false;
|
||||
CommandManager.InvalidateRequerySuggested();
|
||||
}
|
||||
}
|
||||
|
||||
public event EventHandler? CanExecuteChanged
|
||||
{
|
||||
add { CommandManager.RequerySuggested += value; }
|
||||
remove { CommandManager.RequerySuggested -= value; }
|
||||
}
|
||||
}
|
||||
}
|
||||
89
XLIMS.MVVM/Base/RelayCommand.cs
Normal file
89
XLIMS.MVVM/Base/RelayCommand.cs
Normal file
@@ -0,0 +1,89 @@
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Windows.Input;
|
||||
|
||||
namespace XLIMS.MVVM.Base
|
||||
{
|
||||
public class RelayCommand : ICommand
|
||||
{
|
||||
#region Fields
|
||||
|
||||
readonly Action<object> _execute;
|
||||
readonly Predicate<object> _canExecute;
|
||||
|
||||
#endregion // Fields
|
||||
|
||||
#region Constructors
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new command that can always execute.
|
||||
/// </summary>
|
||||
/// <param name="execute">The execution logic.</param>
|
||||
public RelayCommand(Action<object> execute)
|
||||
: this(execute, null)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new command.
|
||||
/// </summary>
|
||||
/// <param name="execute">The execution logic.</param>
|
||||
/// <param name="canExecute">The execution status logic.</param>
|
||||
public RelayCommand(Action<object> execute, Predicate<object> canExecute)
|
||||
{
|
||||
if (execute == null)
|
||||
throw new ArgumentNullException("execute");
|
||||
|
||||
_execute = execute;
|
||||
_canExecute = canExecute;
|
||||
}
|
||||
|
||||
#endregion // Constructors
|
||||
|
||||
#region ICommand Members
|
||||
|
||||
[DebuggerStepThrough]
|
||||
public bool CanExecute(object parameter)
|
||||
{
|
||||
return _canExecute == null ? true : _canExecute(parameter);
|
||||
}
|
||||
|
||||
public event EventHandler CanExecuteChanged
|
||||
{
|
||||
add { CommandManager.RequerySuggested += value; }
|
||||
remove { CommandManager.RequerySuggested -= value; }
|
||||
}
|
||||
|
||||
public void Execute(object parameter)
|
||||
{
|
||||
_execute(parameter);
|
||||
}
|
||||
|
||||
#endregion // ICommand Members
|
||||
}
|
||||
|
||||
public class RelayCommand<T> : ICommand
|
||||
{
|
||||
private readonly Action<T> _execute;
|
||||
private readonly Func<T, bool> _canExecute;
|
||||
|
||||
public RelayCommand(Action<T> execute, Func<T, bool> canExecute = null)
|
||||
{
|
||||
_execute = execute;
|
||||
_canExecute = canExecute;
|
||||
}
|
||||
|
||||
public bool CanExecute(object parameter)
|
||||
=> _canExecute == null || _canExecute((T)parameter);
|
||||
|
||||
public void Execute(object parameter)
|
||||
=> _execute((T)parameter);
|
||||
|
||||
public event EventHandler CanExecuteChanged
|
||||
{
|
||||
add => CommandManager.RequerySuggested += value;
|
||||
remove => CommandManager.RequerySuggested -= value;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
25
XLIMS.MVVM/Base/ViewModelBase.cs
Normal file
25
XLIMS.MVVM/Base/ViewModelBase.cs
Normal file
@@ -0,0 +1,25 @@
|
||||
using System;
|
||||
using System.ComponentModel;
|
||||
using System.Linq.Expressions;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace XLIMS.MVVM.Base
|
||||
{
|
||||
public abstract class ViewModelBase : INotifyPropertyChanged
|
||||
{
|
||||
public event PropertyChangedEventHandler PropertyChanged;
|
||||
|
||||
protected void OnPropertyChanged([CallerMemberName] String propertyName="")
|
||||
{
|
||||
PropertyChangedEventHandler handler = PropertyChanged;
|
||||
|
||||
if (handler != null)
|
||||
{
|
||||
handler(this, new PropertyChangedEventArgs(propertyName));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user