Permalink
Switch branches/tags
tag-test3 tag-test2 tag-test1 release-3.2.0 release-3.1.0-sr3 release-3.1.0-sr2 release-3.1.0-sr1.1 release-3.1.0-sr1 release-3.1.0-rc1 release-3.0.0-sr4.1 release-3.0.0-sr4 release-3.0.0-sr3 release-3.0.0-sr2 release-3.0.0-sr1 release-3.0.0-rc2 release-3.0.0-rc1 release-2.5.1 release-2.5.1-sr4 release-2.5.1-sr3 release-2.5.1-sr2 release-2.5.1-sr1 release-2.5.0 release-2.5.0-sr5 release-2.5.0-sr4 release-2.5.0-sr3 release-2.5.0-sr2 release-2.5.0-sr1 release-2.4.99-rc1 release-2.4.0 release-2.4.0-sr6 release-2.4.0-sr5 release-2.4.0-sr4 release-2.4.0-sr3 release-2.4.0-sr2 release-2.4.0-sr1 release-2.4.0-rc2 release-2.3.4 release-2.3.4-sr6 release-2.3.4-sr5 release-2.3.4-sr4 release-2.3.4-sr3 release-2.3.4-sr2 release-2.3.4-sr1 release-2.3.3 release-2.3.3-sr2 release-2.3.3-sr1.1 release-2.3.3-sr1 release-2.3.3-hf1 release-2.3.2 release-2.3.2-hf1 release-2.3.1 release-2.3.0 release-2.3.0-hf1 release-2.2.0 release-2.2.0-hf1 beta-cycle6buildtest beta-9.9.999-rui beta-9.9.99-rui beta-9.9.9-rui beta-9.9.8-rui beta-3.3.0-pre1 beta-3.2.0-pre3 beta-3.2.0-pre2 beta-3.2.0-pre1.1 beta-3.2.0-pre1 beta-3.1.0-pre4.1 beta-3.1.0-pre4 beta-3.1.0-pre3 beta-3.1.0-pre2.3 beta-3.1.0-pre2.2 beta-3.1.0-pre2 beta-3.1.0-pre1.2 beta-3.1.0-pre1 beta-3.0.0-rui beta-3.0.0-pre4.4 beta-3.0.0-pre4.3 beta-3.0.0-pre4.2 beta-3.0.0-pre4.1 beta-3.0.0-pre4 beta-3.0.0-pre3 beta-3.0.0-pre2.2 beta-3.0.0-pre2 beta-3.0.0-pre1.2 beta-3.0.0-pre1 beta-2.5.1-pre3 beta-2.5.1-pre2 beta-2.5.1-pre1 beta-2.5.0-pre3 beta-2.5.0-pre2 beta-2.5.0-pre1 beta-2.4.1-rui beta-2.4.0-pre3 beta-2.4.0-pre2 beta-2.4.0-pre1 beta-2.3.6-complete-unstable beta-2.3.5-pre6 beta-2.3.5-pre5 beta-2.3.5-pre4 beta-2.3.5-pre3 beta-2.3.5-pre2
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
116 lines (98 sloc) 2.39 KB
using System;
using System.Reflection;
using System.Windows.Input;
namespace Xamarin.Forms
{
public sealed class Command<T> : Command
{
public Command(Action<T> execute)
: base(o =>
{
if (IsValidParameter(o))
{
execute((T)o);
}
})
{
if (execute == null)
{
throw new ArgumentNullException(nameof(execute));
}
}
public Command(Action<T> execute, Func<T, bool> canExecute)
: base(o =>
{
if (IsValidParameter(o))
{
execute((T)o);
}
}, o => IsValidParameter(o) && canExecute((T)o))
{
if (execute == null)
throw new ArgumentNullException(nameof(execute));
if (canExecute == null)
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;
}
}
public class Command : ICommand
{
readonly Func<object, bool> _canExecute;
readonly Action<object> _execute;
public Command(Action<object> execute)
{
if (execute == null)
throw new ArgumentNullException(nameof(execute));
_execute = execute;
}
public Command(Action execute) : this(o => execute())
{
if (execute == null)
throw new ArgumentNullException(nameof(execute));
}
public Command(Action<object> execute, Func<object, bool> canExecute) : this(execute)
{
if (canExecute == null)
throw new ArgumentNullException(nameof(canExecute));
_canExecute = canExecute;
}
public Command(Action execute, Func<bool> canExecute) : this(o => execute(), o => canExecute())
{
if (execute == null)
throw new ArgumentNullException(nameof(execute));
if (canExecute == null)
throw new ArgumentNullException(nameof(canExecute));
}
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;
changed?.Invoke(this, EventArgs.Empty);
}
}
}