我今天早些時候編寫了這段代碼.該代碼的目的是實現(xiàn)ICommand,使其僅一次僅運行一個方法,而對其他方法靜默返回.
我編寫它的目的是為了與多平臺用戶界面一起使用,以便不記錄多次按鍵.
它有大量的構(gòu)造函數(shù),而且我不太了解這種重載.我將在下個學(xué)期學(xué)習(xí)設(shè)計模式,然后在本學(xué)期學(xué)習(xí)面向?qū)ο蟮木幊?因此希望在此之后我的代碼會更干凈!
我有辦法縮短代碼(減少所需的構(gòu)造函數(shù)的數(shù)量)嗎?
using System;
using Xamarin.Forms;
using System.Windows.Input;
using System.Threading.Tasks;
namespace dobjenkins
{
public interface IAsyncCommand : ICommand
{
Task ExecuteAsync (object parameter);
}
public class AsyncCommand : IAsyncCommand
{
private readonly Func<object, Task> execute;
private readonly Func<object, bool> canExecute;
///
/// Constructors and initializors
///
protected AsyncCommand ()
{
}
public AsyncCommand (Func<object, Task> execute, Func<object, bool> canExecute)
{
this.execute = execute;
this.canExecute = canExecute;
}
public AsyncCommand (Func<object, Task> execute)
{
this.execute = execute;
}
public AsyncCommand (Func<Task> execute, Func<bool> canExecute)
{
this.execute = _ => execute ();
this.canExecute = _ => canExecute ();
}
public AsyncCommand (Func<Task> execute)
{
this.execute = _ => execute ();
}
///
/// Execute Methods
///
public async Task ExecuteAsync (object parameter)
{
await execute (parameter);
}
public async void Execute (object parameter)
{
await ExecuteAsync (parameter);
}
///
/// CanExecute methods/Event
///
public event EventHandler CanExecuteChanged;
public void ChangeCanExecute ()
{
var ev = CanExecuteChanged;
if (ev != null) {
ev (this, EventArgs.Empty);
}
}
public bool CanExecute (object parameter)
{
return canExecute == null || canExecute (parameter);
}
}
public sealed class AsyncCommand<T> : AsyncCommand
{
private readonly Func<T, Task> execute;
private readonly Func<T, bool> canExecute;
public AsyncCommand (Func<T, Task> execute)
{
this.execute = execute;
}
public AsyncCommand (Func<T, Task> execute, Func<T, bool> canExecute)
{
this.execute = execute;
this.canExecute = canExecute;
}
///
/// Execute Methods
///
public async Task ExecuteAsync (T parameter)
{
await execute (parameter);
}
public async void Execute (T parameter)
{
await ExecuteAsync (parameter);
}
public bool CanExecute (T parameter)
{
return canExecute == null || canExecute (parameter);
}
}
public class ExclusiveCommand : ICommand
{
protected ICommand Backing;
protected static bool IsBusy = false;
//
// Constructors
//
#region Constructors
public ExclusiveCommand()
{
}
//
// SYNC (normal) CONSTRUCTORS
//
public ExclusiveCommand (Action<object> execute, Func<object, bool> canExecute)
{
Backing = new Command (execute, canExecute);
}
public ExclusiveCommand (Action<object> execute)
{
Backing = new Command (execute);
}
public ExclusiveCommand (Action execute, Func<bool> canExecute)
{
Backing = new Command (execute, canExecute);
}
public ExclusiveCommand (Action execute)
{
Backing = new Command (execute);
}
//
// ASYNC CONSTRUCTORS
//
public ExclusiveCommand (Func<object, Task> execute, Func<object, bool> canExecute)
{
Backing = new AsyncCommand (execute, canExecute);
}
public ExclusiveCommand (Func<object, Task> execute)
{
Backing = new AsyncCommand (execute);
}
public ExclusiveCommand (Func<Task> execute, Func<bool> canExecute)
{
Backing = new AsyncCommand (execute, canExecute);
}
public ExclusiveCommand (Func<Task> a)
{
Backing = new AsyncCommand (a);
}
//
// End Constructors
//
#endregion Constructors
// Execute
public async void Execute (object parameter)
{
if (IsBusy) {
return;
}
IsBusy = true;
var async = Backing as AsyncCommand;
if (async != null) {
await async.ExecuteAsync (parameter);
} else {
Backing.Execute (parameter);
}
IsBusy = false;
}
//
/// Can execute
//
public event EventHandler CanExecuteChanged;
public void ChangeCanExecute ()
{
var ev = CanExecuteChanged;
if (ev != null) {
ev (this, EventArgs.Empty);
}
}
public bool CanExecute (object parameter)
{
return Backing.CanExecute (parameter);
}
}
}
public sealed class ExclusiveCommand<T> : ExclusiveCommand
{
///
/// Constructors
///
#region Constructors
public ExclusiveCommand()
{
}
//
// SYNC (normal) CONSTRUCTORS
//
public ExclusiveCommand (Action<T> execute, Func<T, bool> canExecute)
{
Backing = new Command<T> (execute, canExecute);
}
public ExclusiveCommand (Action<T> execute)
{
Backing = new Command<T> (execute);
}
//
// ASYNC CONSTRUCTORS
//
public ExclusiveCommand (Func<T, Task> execute, Func<T, bool> canExecute)
{
Backing = new AsyncCommand<T> (execute, canExecute);
}
public ExclusiveCommand (Func<T, Task> execute)
{
Backing = new AsyncCommand<T> (execute);
}
//
// End Constructors
//
#endregion Constructors
}
解決方法: 您可以像這樣使用可選參數(shù):
public AsyncCommand (Func<object, Task> execute, Func<object, bool> canExecute = null)
{
this.execute = execute;
this.canExecute = canExecute;
}
這將允許您刪除AsyncCommand(Func< object,Task> execute)構(gòu)造函數(shù).
您也可以只將Func< object,Task>重載并刪除Func< Task>重載,并要求客戶編寫_ =>東西lambda.但這可能會接受,也可能無法接受,具體取決于您的要求. 來源:https://www./content-4-529251.html
|