Modified SetCommand to allow avoiding explicit type parameter when using an
Android CheckBox and the default CheckedChange event.
This commit is contained in:
Родитель
0f60739e36
Коммит
48064a528d
|
@ -272,15 +272,7 @@ namespace GalaSoft.MvvmLight.Helpers
|
|||
|
||||
var castedBinding = (Binding<T, T>)commandParameterBinding;
|
||||
|
||||
EventHandler handler = (s, args) =>
|
||||
{
|
||||
var param = castedBinding == null ? default(T) : castedBinding.Value;
|
||||
|
||||
if (command.CanExecute(param))
|
||||
{
|
||||
command.Execute(param);
|
||||
}
|
||||
};
|
||||
var handler = e.GetCommandHandler(eventName, t, command, castedBinding);
|
||||
|
||||
e.AddEventHandler(
|
||||
element,
|
||||
|
@ -378,13 +370,7 @@ namespace GalaSoft.MvvmLight.Helpers
|
|||
var t = element.GetType();
|
||||
var e = t.GetEventInfoForControl(eventName);
|
||||
|
||||
EventHandler handler = (s, args) =>
|
||||
{
|
||||
if (command.CanExecute(null))
|
||||
{
|
||||
command.Execute(null);
|
||||
}
|
||||
};
|
||||
var handler = e.GetCommandHandler(eventName, t, command);
|
||||
|
||||
e.AddEventHandler(
|
||||
element,
|
||||
|
@ -460,13 +446,7 @@ namespace GalaSoft.MvvmLight.Helpers
|
|||
var t = element.GetType();
|
||||
var e = t.GetEventInfoForControl(eventName);
|
||||
|
||||
EventHandler handler = (s, args) =>
|
||||
{
|
||||
if (command.CanExecute(commandParameter))
|
||||
{
|
||||
command.Execute(commandParameter);
|
||||
}
|
||||
};
|
||||
var handler = e.GetCommandHandler(eventName, t, command, commandParameter);
|
||||
|
||||
e.AddEventHandler(
|
||||
element,
|
||||
|
|
|
@ -16,8 +16,11 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Reflection;
|
||||
using System.Windows.Input;
|
||||
using Android.Views;
|
||||
using Android.Widget;
|
||||
using GalaSoft.MvvmLight.Command;
|
||||
|
||||
namespace GalaSoft.MvvmLight.Helpers
|
||||
{
|
||||
|
@ -84,5 +87,100 @@ namespace GalaSoft.MvvmLight.Helpers
|
|||
|
||||
return eventName;
|
||||
}
|
||||
|
||||
internal static Delegate GetCommandHandler(
|
||||
this EventInfo info,
|
||||
string eventName,
|
||||
Type elementType,
|
||||
ICommand command)
|
||||
{
|
||||
Delegate result;
|
||||
|
||||
if (string.IsNullOrEmpty(eventName)
|
||||
&& elementType == typeof (CheckBox))
|
||||
{
|
||||
EventHandler<CompoundButton.CheckedChangeEventArgs> handler = (s, args) =>
|
||||
{
|
||||
if (command.CanExecute(null))
|
||||
{
|
||||
command.Execute(null);
|
||||
}
|
||||
};
|
||||
|
||||
result = handler;
|
||||
}
|
||||
else
|
||||
{
|
||||
EventHandler handler = (s, args) =>
|
||||
{
|
||||
if (command.CanExecute(null))
|
||||
{
|
||||
command.Execute(null);
|
||||
}
|
||||
};
|
||||
|
||||
result = handler;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
internal static Delegate GetCommandHandler<T>(
|
||||
this EventInfo info,
|
||||
string eventName,
|
||||
Type elementType,
|
||||
RelayCommand<T> command,
|
||||
Binding<T, T> castedBinding)
|
||||
{
|
||||
Delegate result;
|
||||
|
||||
if (string.IsNullOrEmpty(eventName)
|
||||
&& elementType == typeof(CheckBox))
|
||||
{
|
||||
EventHandler<CompoundButton.CheckedChangeEventArgs> handler = (s, args) =>
|
||||
{
|
||||
var param = castedBinding == null ? default(T) : castedBinding.Value;
|
||||
command.Execute(param);
|
||||
};
|
||||
|
||||
result = handler;
|
||||
}
|
||||
else
|
||||
{
|
||||
EventHandler handler = (s, args) =>
|
||||
{
|
||||
var param = castedBinding == null ? default(T) : castedBinding.Value;
|
||||
command.Execute(param);
|
||||
};
|
||||
|
||||
result = handler;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
internal static Delegate GetCommandHandler<T>(
|
||||
this EventInfo info,
|
||||
string eventName,
|
||||
Type elementType,
|
||||
RelayCommand<T> command,
|
||||
T commandParameter)
|
||||
{
|
||||
Delegate result;
|
||||
|
||||
if (string.IsNullOrEmpty(eventName)
|
||||
&& elementType == typeof(CheckBox))
|
||||
{
|
||||
EventHandler<CompoundButton.CheckedChangeEventArgs> handler = (s, args) => command.Execute(commandParameter);
|
||||
result = handler;
|
||||
}
|
||||
else
|
||||
{
|
||||
EventHandler handler = (s, args) => command.Execute(commandParameter);
|
||||
result = handler;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -16,7 +16,10 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Reflection;
|
||||
using System.Windows.Input;
|
||||
using Foundation;
|
||||
using GalaSoft.MvvmLight.Command;
|
||||
using UIKit;
|
||||
|
||||
namespace GalaSoft.MvvmLight.Helpers
|
||||
|
@ -349,5 +352,59 @@ namespace GalaSoft.MvvmLight.Helpers
|
|||
|
||||
return eventName;
|
||||
}
|
||||
|
||||
|
||||
internal static Delegate GetCommandHandler(
|
||||
this EventInfo info,
|
||||
string eventName,
|
||||
Type elementType,
|
||||
ICommand command)
|
||||
{
|
||||
// At the moment, all supported controls with default events
|
||||
// in iOS are using EventHandler, and not EventHandler<...>.
|
||||
|
||||
EventHandler handler = (s, args) =>
|
||||
{
|
||||
if (command.CanExecute(null))
|
||||
{
|
||||
command.Execute(null);
|
||||
}
|
||||
};
|
||||
|
||||
return handler;
|
||||
}
|
||||
|
||||
internal static Delegate GetCommandHandler<T>(
|
||||
this EventInfo info,
|
||||
string eventName,
|
||||
Type elementType,
|
||||
RelayCommand<T> command,
|
||||
Binding<T, T> castedBinding)
|
||||
{
|
||||
// At the moment, all supported controls with default events
|
||||
// in iOS are using EventHandler, and not EventHandler<...>.
|
||||
|
||||
EventHandler handler = (s, args) =>
|
||||
{
|
||||
var param = castedBinding == null ? default(T) : castedBinding.Value;
|
||||
command.Execute(param);
|
||||
};
|
||||
|
||||
return handler;
|
||||
}
|
||||
|
||||
internal static Delegate GetCommandHandler<T>(
|
||||
this EventInfo info,
|
||||
string eventName,
|
||||
Type elementType,
|
||||
RelayCommand<T> command,
|
||||
T commandParameter)
|
||||
{
|
||||
// At the moment, all supported controls with default events
|
||||
// in iOS are using EventHandler, and not EventHandler<...>.
|
||||
|
||||
EventHandler handler = (s, args) => command.Execute(commandParameter);
|
||||
return handler;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -207,7 +207,7 @@ namespace GalaSoft.MvvmLight.Test.Binding
|
|||
}
|
||||
|
||||
[Test]
|
||||
public void SetCommand_OnCheckBoxNoValueNoEventName_ClickEventShouldBeUsed()
|
||||
public void SetCommand_OnCheckBoxNoValueNoEventName_CheckedChangeEventShouldBeUsed()
|
||||
{
|
||||
var value = DateTime.Now.Ticks.ToString();
|
||||
var vmTarget = new TestViewModel();
|
||||
|
@ -215,7 +215,7 @@ namespace GalaSoft.MvvmLight.Test.Binding
|
|||
|
||||
var checkBox = new CheckBox(Application.Context);
|
||||
|
||||
checkBox.SetCommand<CompoundButton.CheckedChangeEventArgs>(vmTarget.SetPropertyWithoutValueCommand);
|
||||
checkBox.SetCommand(vmTarget.SetPropertyWithoutValueCommand);
|
||||
|
||||
Assert.IsNull(vmTarget.TargetProperty);
|
||||
checkBox.PerformClick();
|
||||
|
@ -279,7 +279,7 @@ namespace GalaSoft.MvvmLight.Test.Binding
|
|||
vmSource,
|
||||
() => vmSource.Model.MyProperty);
|
||||
|
||||
checkBox.SetCommand<string, CompoundButton.CheckedChangeEventArgs>(
|
||||
checkBox.SetCommand(
|
||||
vmTarget.SetPropertyCommand,
|
||||
_binding);
|
||||
|
||||
|
@ -319,7 +319,7 @@ namespace GalaSoft.MvvmLight.Test.Binding
|
|||
|
||||
var checkBox = new CheckBox(Application.Context);
|
||||
|
||||
checkBox.SetCommand<string, CompoundButton.CheckedChangeEventArgs>(
|
||||
checkBox.SetCommand(
|
||||
vmTarget.SetPropertyCommand,
|
||||
value);
|
||||
|
||||
|
@ -379,7 +379,7 @@ namespace GalaSoft.MvvmLight.Test.Binding
|
|||
var vmTarget = new TestViewModel();
|
||||
var checkBox = new CheckBox(Application.Context);
|
||||
|
||||
checkBox.SetCommand<CompoundButton.CheckedChangeEventArgs>(vmTarget.TestCommandImpl);
|
||||
checkBox.SetCommand(vmTarget.TestCommandImpl);
|
||||
|
||||
var castedCommand = (CommandImpl)vmTarget.TestCommandImpl;
|
||||
|
||||
|
|
Загрузка…
Ссылка в новой задаче