2016-03-22 23:02:25 +03:00
|
|
|
|
using System;
|
2016-12-02 00:15:17 +03:00
|
|
|
|
using System.Reflection;
|
2016-03-22 23:02:25 +03:00
|
|
|
|
using System.Windows.Input;
|
|
|
|
|
|
|
|
|
|
namespace Xamarin.Forms
|
|
|
|
|
{
|
2016-12-02 00:15:17 +03:00
|
|
|
|
public sealed class Command<T> : Command
|
2016-03-22 23:02:25 +03:00
|
|
|
|
{
|
2016-12-02 00:15:17 +03:00
|
|
|
|
public Command(Action<T> execute)
|
|
|
|
|
: base(o =>
|
|
|
|
|
{
|
|
|
|
|
if (IsValidParameter(o))
|
|
|
|
|
{
|
|
|
|
|
execute((T)o);
|
|
|
|
|
}
|
|
|
|
|
})
|
2016-03-22 23:02:25 +03:00
|
|
|
|
{
|
|
|
|
|
if (execute == null)
|
2016-12-02 00:15:17 +03:00
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(execute));
|
|
|
|
|
}
|
2016-03-22 23:02:25 +03:00
|
|
|
|
}
|
|
|
|
|
|
2016-12-02 00:15:17 +03:00
|
|
|
|
public Command(Action<T> execute, Func<T, bool> canExecute)
|
|
|
|
|
: base(o =>
|
|
|
|
|
{
|
|
|
|
|
if (IsValidParameter(o))
|
|
|
|
|
{
|
|
|
|
|
execute((T)o);
|
|
|
|
|
}
|
|
|
|
|
}, o => IsValidParameter(o) && canExecute((T)o))
|
2016-03-22 23:02:25 +03:00
|
|
|
|
{
|
|
|
|
|
if (execute == null)
|
2016-12-02 00:15:17 +03:00
|
|
|
|
throw new ArgumentNullException(nameof(execute));
|
2016-03-22 23:02:25 +03:00
|
|
|
|
if (canExecute == null)
|
2016-12-02 00:15:17 +03:00
|
|
|
|
throw new ArgumentNullException(nameof(canExecute));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool IsValidParameter(object o)
|
|
|
|
|
{
|
|
|
|
|
if (o != null)
|
|
|
|
|
{
|
|
|
|
|
// The parameter isn't null, so we don't have to worry whether null is a valid option
|
|
|
|
|
return o is T;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var t = typeof(T);
|
|
|
|
|
|
|
|
|
|
// The parameter is null. Is T Nullable?
|
|
|
|
|
if (Nullable.GetUnderlyingType(t) != null)
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Not a Nullable, if it's a value type then null is not valid
|
|
|
|
|
return !t.GetTypeInfo().IsValueType;
|
2016-03-22 23:02:25 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class Command : ICommand
|
|
|
|
|
{
|
|
|
|
|
readonly Func<object, bool> _canExecute;
|
|
|
|
|
readonly Action<object> _execute;
|
|
|
|
|
|
|
|
|
|
public Command(Action<object> execute)
|
|
|
|
|
{
|
|
|
|
|
if (execute == null)
|
2016-12-02 00:15:17 +03:00
|
|
|
|
throw new ArgumentNullException(nameof(execute));
|
2016-03-22 23:02:25 +03:00
|
|
|
|
|
|
|
|
|
_execute = execute;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Command(Action execute) : this(o => execute())
|
|
|
|
|
{
|
|
|
|
|
if (execute == null)
|
2016-12-02 00:15:17 +03:00
|
|
|
|
throw new ArgumentNullException(nameof(execute));
|
2016-03-22 23:02:25 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Command(Action<object> execute, Func<object, bool> canExecute) : this(execute)
|
|
|
|
|
{
|
|
|
|
|
if (canExecute == null)
|
2016-12-02 00:15:17 +03:00
|
|
|
|
throw new ArgumentNullException(nameof(canExecute));
|
2016-03-22 23:02:25 +03:00
|
|
|
|
|
|
|
|
|
_canExecute = canExecute;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Command(Action execute, Func<bool> canExecute) : this(o => execute(), o => canExecute())
|
|
|
|
|
{
|
|
|
|
|
if (execute == null)
|
2016-12-02 00:15:17 +03:00
|
|
|
|
throw new ArgumentNullException(nameof(execute));
|
2016-03-22 23:02:25 +03:00
|
|
|
|
if (canExecute == null)
|
2016-12-02 00:15:17 +03:00
|
|
|
|
throw new ArgumentNullException(nameof(canExecute));
|
2016-03-22 23:02:25 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool CanExecute(object parameter)
|
|
|
|
|
{
|
|
|
|
|
if (_canExecute != null)
|
|
|
|
|
return _canExecute(parameter);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public event EventHandler CanExecuteChanged;
|
|
|
|
|
|
|
|
|
|
public void Execute(object parameter)
|
|
|
|
|
{
|
|
|
|
|
_execute(parameter);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void ChangeCanExecute()
|
|
|
|
|
{
|
|
|
|
|
EventHandler changed = CanExecuteChanged;
|
2016-12-02 00:15:17 +03:00
|
|
|
|
changed?.Invoke(this, EventArgs.Empty);
|
2016-03-22 23:02:25 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|