Added a cleaner way to convert event args in EventToCommand

This commit is contained in:
Laurent Bugnion 2013-09-23 13:44:58 +02:00
Родитель d226f50edb
Коммит 30dc6dca24
24 изменённых файлов: 925 добавлений и 292 удалений

Просмотреть файл

@ -33,8 +33,8 @@ namespace GalaSoft.MvvmLight.Command
/// and leave the CommandParameter and CommandParameterValue empty!</para>
/// </summary>
////[ClassInfo(typeof(EventToCommand),
//// VersionString = "4.1.5",
//// DateString = "201212161200",
//// VersionString = "4.1.6",
//// DateString = "201305182300",
//// Description = "A Trigger used to bind any event to an ICommand.",
//// UrlContacts = "http://www.galasoft.ch/contact_en.html",
//// Email = "laurent@galasoft.ch")]
@ -54,6 +54,48 @@ namespace GalaSoft.MvvmLight.Command
set;
}
/// <summary>
/// Gets or sets a converter used to convert the EventArgs when using
/// <see cref="PassEventArgsToCommand"/>. If PassEventArgsToCommand is false,
/// this property is never used.
/// </summary>
public IEventArgsConverter EventArgsConverter
{
get;
set;
}
/// <summary>
/// The <see cref="EventArgsConverterParameter" /> dependency property's name.
/// </summary>
public const string EventArgsConverterParameterPropertyName = "EventArgsConverterParameter";
/// <summary>
/// Gets or sets a parameters for the converter used to convert the EventArgs when using
/// <see cref="PassEventArgsToCommand"/>. If PassEventArgsToCommand is false,
/// this property is never used. This is a dependency property.
/// </summary>
public object EventArgsConverterParameter
{
get
{
return GetValue(EventArgsConverterParameterProperty);
}
set
{
SetValue(EventArgsConverterParameterProperty, value);
}
}
/// <summary>
/// Identifies the <see cref="EventArgsConverterParameter" /> dependency property.
/// </summary>
public static readonly DependencyProperty EventArgsConverterParameterProperty = DependencyProperty.Register(
EventArgsConverterParameterPropertyName,
typeof(object),
typeof(EventToCommand),
new UIPropertyMetadata(null));
/// <summary>
/// Provides a simple way to invoke this trigger programatically
/// without any EventArgs.
@ -82,7 +124,9 @@ namespace GalaSoft.MvvmLight.Command
if (commandParameter == null
&& PassEventArgsToCommand)
{
commandParameter = parameter;
commandParameter = EventArgsConverter == null
? parameter
: EventArgsConverter.Convert(parameter, EventArgsConverterParameter);
}
if (command != null
@ -133,12 +177,12 @@ namespace GalaSoft.MvvmLight.Command
return;
}
var command = this.GetCommand();
var command = GetCommand();
if (this.MustToggleIsEnabledValue
if (MustToggleIsEnabledValue
&& command != null)
{
element.IsEnabled = command.CanExecute(this.CommandParameterValue);
element.IsEnabled = command.CanExecute(CommandParameterValue);
}
}

Просмотреть файл

@ -0,0 +1,23 @@
namespace GalaSoft.MvvmLight.Command
{
/// <summary>
/// The definition of the converter used to convert an EventArgs
/// in the <see cref="EventToCommand"/> class, if the
/// <see cref="EventToCommand.PassEventArgsToCommand"/> property is true.
/// Set an instance of this class to the <see cref="EventToCommand.EventArgsConverter"/>
/// property of the EventToCommand instance.
/// </summary>
public interface IEventArgsConverter
{
/// <summary>
/// The method used to convert the EventArgs instance.
/// </summary>
/// <param name="value">An instance of EventArgs passed by the
/// event that the EventToCommand instance is handling.</param>
/// <param name="parameter">An optional parameter used for the conversion. Use
/// the <see cref="EventToCommand.EventArgsConverterParameter"/> property
/// to set this value. This may be null.</param>
/// <returns>The converted value.</returns>
object Convert(object value, object parameter);
}
}

Просмотреть файл

@ -96,6 +96,7 @@
<ItemGroup>
<Compile Include="Command\EventToCommand.cs" />
<Compile Include="Command\EventToCommand.WPF.cs" />
<Compile Include="Command\IEventArgsConverter.cs" />
<Compile Include="Ioc\ISimpleIoc.cs" />
<Compile Include="Ioc\PreferredConstructor.cs" />
<Compile Include="Ioc\SimpleIoc.cs" />

Просмотреть файл

@ -36,8 +36,8 @@ namespace GalaSoft.MvvmLight.Command
/// and leave the CommandParameter and CommandParameterValue empty!</para>
/// </summary>
////[ClassInfo(typeof(EventToCommand),
//// VersionString = "4.1.5",
//// DateString = "201212161200",
//// VersionString = "4.1.6",
//// DateString = "201305182300",
//// Description = "A Trigger used to bind any event to an ICommand.",
//// UrlContacts = "http://www.galasoft.ch/contact_en.html",
//// Email = "laurent@galasoft.ch")]
@ -257,6 +257,48 @@ namespace GalaSoft.MvvmLight.Command
set;
}
/// <summary>
/// Gets or sets a converter used to convert the EventArgs when using
/// <see cref="PassEventArgsToCommand"/>. If PassEventArgsToCommand is false,
/// this property is never used.
/// </summary>
public IEventArgsConverter EventArgsConverter
{
get;
set;
}
/// <summary>
/// The <see cref="EventArgsConverterParameter" /> dependency property's name.
/// </summary>
public const string EventArgsConverterParameterPropertyName = "EventArgsConverterParameter";
/// <summary>
/// Gets or sets a parameters for the converter used to convert the EventArgs when using
/// <see cref="PassEventArgsToCommand"/>. If PassEventArgsToCommand is false,
/// this property is never used. This is a dependency property.
/// </summary>
public object EventArgsConverterParameter
{
get
{
return GetValue(EventArgsConverterParameterProperty);
}
set
{
SetValue(EventArgsConverterParameterProperty, value);
}
}
/// <summary>
/// Identifies the <see cref="EventArgsConverterParameter" /> dependency property.
/// </summary>
public static readonly DependencyProperty EventArgsConverterParameterProperty = DependencyProperty.Register(
EventArgsConverterParameterPropertyName,
typeof(object),
typeof(EventToCommand),
new PropertyMetadata(null));
/// <summary>
/// Provides a simple way to invoke this trigger programatically
/// without any EventArgs.
@ -285,7 +327,9 @@ namespace GalaSoft.MvvmLight.Command
if (commandParameter == null
&& PassEventArgsToCommand)
{
commandParameter = parameter;
commandParameter = EventArgsConverter == null
? parameter
: EventArgsConverter.Convert(parameter, EventArgsConverterParameter);
}
if (command != null

Просмотреть файл

@ -66,6 +66,9 @@
<Reference Include="WindowsBase" />
</ItemGroup>
<ItemGroup>
<Compile Include="..\GalaSoft.MvvmLight.Extras %28NET35%29\Command\IEventArgsConverter.cs">
<Link>Command\IEventArgsConverter.cs</Link>
</Compile>
<Compile Include="..\GalaSoft.MvvmLight.Extras %28NET35%29\Ioc\ISimpleIoc.cs">
<Link>Ioc\ISimpleIoc.cs</Link>
</Compile>

Просмотреть файл

@ -58,6 +58,9 @@
<Reference Include="WindowsBase" />
</ItemGroup>
<ItemGroup>
<Compile Include="..\GalaSoft.MvvmLight.Extras %28NET35%29\Command\IEventArgsConverter.cs">
<Link>Command\IEventArgsConverter.cs</Link>
</Compile>
<Compile Include="..\GalaSoft.MvvmLight.Extras %28NET35%29\Ioc\ISimpleIoc.cs">
<Link>Ioc\ISimpleIoc.cs</Link>
</Compile>

Просмотреть файл

@ -67,6 +67,9 @@
<Reference Include="System.Windows.Browser" />
</ItemGroup>
<ItemGroup>
<Compile Include="..\GalaSoft.MvvmLight.Extras %28NET35%29\Command\IEventArgsConverter.cs">
<Link>Command\IEventArgsConverter.cs</Link>
</Compile>
<Compile Include="..\GalaSoft.MvvmLight.Extras %28NET35%29\Ioc\ISimpleIoc.cs">
<Link>Ioc\ISimpleIoc.cs</Link>
</Compile>

Просмотреть файл

@ -69,6 +69,9 @@
<Reference Include="System.Windows.Browser" />
</ItemGroup>
<ItemGroup>
<Compile Include="..\GalaSoft.MvvmLight.Extras %28NET35%29\Command\IEventArgsConverter.cs">
<Link>Command\IEventArgsConverter.cs</Link>
</Compile>
<Compile Include="..\GalaSoft.MvvmLight.Extras %28NET35%29\Ioc\ISimpleIoc.cs">
<Link>Ioc\ISimpleIoc.cs</Link>
</Compile>

Просмотреть файл

@ -57,6 +57,9 @@
<Reference Include="mscorlib.extensions" />
</ItemGroup>
<ItemGroup>
<Compile Include="..\GalaSoft.MvvmLight.Extras %28NET35%29\Command\IEventArgsConverter.cs">
<Link>Command\IEventArgsConverter.cs</Link>
</Compile>
<Compile Include="..\GalaSoft.MvvmLight.Extras %28NET35%29\Ioc\ISimpleIoc.cs">
<Link>Ioc\ISimpleIoc.cs</Link>
</Compile>

Просмотреть файл

@ -85,6 +85,9 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Compile Include="..\GalaSoft.MvvmLight.Extras %28NET35%29\Command\IEventArgsConverter.cs">
<Link>Command\IEventArgsConverter.cs</Link>
</Compile>
<Compile Include="..\GalaSoft.MvvmLight.Extras %28NET35%29\Ioc\ISimpleIoc.cs">
<Link>Ioc\ISimpleIoc.cs</Link>
</Compile>

Просмотреть файл

@ -0,0 +1,25 @@
using System;
using System.Globalization;
using System.Windows.Data;
using GalaSoft.MvvmLight.Command;
namespace GalaSoft.MvvmLight.Test.Command
{
public class TestEventArgsConverter : IEventArgsConverter
{
public string TestPrefix
{
get;
set;
}
public object Convert(object value, object parameter)
{
var args = (StringEventArgs)value;
return TestPrefix
+ args.Parameter
+ (parameter == null ? string.Empty : parameter.ToString());
}
}
}

Просмотреть файл

@ -0,0 +1,526 @@
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Interactivity;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using GalaSoft.MvvmLight.Command;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Windows.Input;
using System;
namespace GalaSoft.MvvmLight.Test.Command
{
[TestClass]
public class EventToCommandEventArgsTest
{
[TestMethod]
public void TestInvokeSimpleCommandWithEventArgs()
{
var trigger = new EventToCommandStub();
var rectangle = new Rectangle();
((IAttachedObject)trigger).Attach(rectangle);
trigger.PassEventArgsToCommand = true;
var vm = new TestViewModel();
var binding = new Binding
{
Source = vm.SimpleCommand
};
#if SL3
trigger.Command = binding;
#else
BindingOperations.SetBinding(trigger, EventToCommand.CommandProperty, binding);
#endif
var args = new StringEventArgs("StringEventArgs");
trigger.InvokeWithEventArgs(args);
Assert.IsTrue(vm.CommandExecuted);
Assert.AreEqual(null, vm.ParameterReceived);
}
[TestMethod]
[ExpectedException(typeof(InvalidCastException))]
public void TestInvokeParameterCommandWithEventArgs()
{
var trigger = new EventToCommandStub();
var rectangle = new Rectangle();
((IAttachedObject)trigger).Attach(rectangle);
trigger.PassEventArgsToCommand = true;
var vm = new TestViewModel();
var binding = new Binding
{
Source = vm.ParameterCommand
};
#if SL3
trigger.Command = binding;
#else
BindingOperations.SetBinding(trigger, EventToCommand.CommandProperty, binding);
#endif
var args = new StringEventArgs("StringEventArgs");
trigger.InvokeWithEventArgs(args);
Assert.IsTrue(vm.CommandExecuted);
Assert.AreEqual(args.Parameter, vm.ParameterReceived);
}
[TestMethod]
public void TestInvokeWithEventArgsAndNoParameter()
{
var trigger = new EventToCommandStub();
var rectangle = new Rectangle();
((IAttachedObject)trigger).Attach(rectangle);
trigger.PassEventArgsToCommand = true;
var vm = new TestViewModel();
var binding = new Binding
{
Source = vm.CommandWithEventArgs
};
#if SL3
trigger.Command = binding;
#else
BindingOperations.SetBinding(trigger, EventToCommand.CommandProperty, binding);
#endif
var args = new StringEventArgs("StringEventArgs");
trigger.InvokeWithEventArgs(args);
Assert.IsTrue(vm.CommandExecuted);
Assert.AreEqual(args.Parameter, vm.ParameterReceived);
}
[TestMethod]
public void TestInvokeWithEventArgsAndParameterValue()
{
var trigger = new EventToCommandStub();
var rectangle = new Rectangle();
((IAttachedObject)trigger).Attach(rectangle);
trigger.PassEventArgsToCommand = true;
var vm = new TestViewModel();
var binding = new Binding
{
Source = vm.ParameterCommand
};
#if SL3
trigger.Command = binding;
#else
BindingOperations.SetBinding(trigger, EventToCommand.CommandProperty, binding);
#endif
const string CommandParameter = "CommandParameter";
trigger.CommandParameterValue = CommandParameter;
var args = new StringEventArgs("StringEventArgs");
trigger.InvokeWithEventArgs(args);
Assert.IsTrue(vm.CommandExecuted);
Assert.AreEqual(CommandParameter, vm.ParameterReceived);
}
[TestMethod]
public void TestInvokeWithEventArgsAndBoundParameter()
{
var trigger = new EventToCommandStub();
var rectangle = new Rectangle();
((IAttachedObject)trigger).Attach(rectangle);
trigger.PassEventArgsToCommand = true;
var vm = new TestViewModel();
var binding = new Binding
{
Source = vm.ParameterCommand
};
var textBox = new TextBox
{
Text = "BoundParameter"
};
var bindingParameter = new Binding
{
Source = textBox,
Path = new PropertyPath("Text")
};
#if SL3
trigger.Command = binding;
trigger.CommandParameter = bindingParameter;
#else
BindingOperations.SetBinding(trigger, EventToCommand.CommandProperty, binding);
BindingOperations.SetBinding(trigger, EventToCommand.CommandParameterProperty, bindingParameter);
#endif
var args = new StringEventArgs("StringEventArgs");
trigger.InvokeWithEventArgs(args);
Assert.IsTrue(vm.CommandExecuted);
Assert.AreEqual(textBox.Text, vm.ParameterReceived);
}
[TestMethod]
public void TestInvokeSimpleCommandWithEventArgsAndConverter()
{
var trigger = new EventToCommandStub();
var rectangle = new Rectangle();
((IAttachedObject)trigger).Attach(rectangle);
const string prefix = "Test";
trigger.PassEventArgsToCommand = true;
trigger.EventArgsConverter = new TestEventArgsConverter
{
TestPrefix = prefix
};
var vm = new TestViewModel();
var binding = new Binding
{
Source = vm.SimpleCommand
};
#if SL3
trigger.Command = binding;
#else
BindingOperations.SetBinding(trigger, EventToCommand.CommandProperty, binding);
#endif
var args = new StringEventArgs("StringEventArgs");
trigger.InvokeWithEventArgs(args);
Assert.IsTrue(vm.CommandExecuted);
Assert.AreEqual(null, vm.ParameterReceived);
}
[TestMethod]
public void TestInvokeParameterCommandWithEventArgsAndConverter()
{
var trigger = new EventToCommandStub();
var rectangle = new Rectangle();
((IAttachedObject)trigger).Attach(rectangle);
const string prefix = "Test";
trigger.PassEventArgsToCommand = true;
trigger.EventArgsConverter = new TestEventArgsConverter
{
TestPrefix = prefix
};
var vm = new TestViewModel();
var binding = new Binding
{
Source = vm.ParameterCommand
};
#if SL3
trigger.Command = binding;
#else
BindingOperations.SetBinding(trigger, EventToCommand.CommandProperty, binding);
#endif
var args = new StringEventArgs("StringEventArgs");
trigger.InvokeWithEventArgs(args);
Assert.IsTrue(vm.CommandExecuted);
Assert.AreEqual(prefix + args.Parameter, vm.ParameterReceived);
}
[TestMethod]
public void TestInvokeWithEventArgsAndNoParameterAndConverter()
{
var trigger = new EventToCommandStub();
var rectangle = new Rectangle();
((IAttachedObject)trigger).Attach(rectangle);
const string prefix = "Test";
trigger.PassEventArgsToCommand = true;
trigger.EventArgsConverter = new TestEventArgsConverter
{
TestPrefix = prefix
};
var vm = new TestViewModel();
var binding = new Binding
{
Source = vm.CommandWithEventArgsConverted
};
#if SL3
trigger.Command = binding;
#else
BindingOperations.SetBinding(trigger, EventToCommand.CommandProperty, binding);
#endif
var args = new StringEventArgs("StringEventArgs");
trigger.InvokeWithEventArgs(args);
Assert.IsTrue(vm.CommandExecuted);
Assert.AreEqual(prefix + args.Parameter, vm.ParameterReceived);
}
[TestMethod]
public void TestInvokeWithEventArgsAndParameterValueAndConverter()
{
var trigger = new EventToCommandStub();
var rectangle = new Rectangle();
((IAttachedObject)trigger).Attach(rectangle);
const string prefix = "Test";
trigger.PassEventArgsToCommand = true;
trigger.EventArgsConverter = new TestEventArgsConverter
{
TestPrefix = prefix
};
var vm = new TestViewModel();
var binding = new Binding
{
Source = vm.ParameterCommand
};
#if SL3
trigger.Command = binding;
#else
BindingOperations.SetBinding(trigger, EventToCommand.CommandProperty, binding);
#endif
const string commandParameter = "CommandParameter";
trigger.CommandParameterValue = commandParameter;
var args = new StringEventArgs("StringEventArgs");
trigger.InvokeWithEventArgs(args);
Assert.IsTrue(vm.CommandExecuted);
Assert.AreEqual(commandParameter, vm.ParameterReceived);
}
[TestMethod]
public void TestInvokeWithEventArgsAndBoundParameterAndConverter()
{
var trigger = new EventToCommandStub();
var rectangle = new Rectangle();
((IAttachedObject)trigger).Attach(rectangle);
const string prefix = "Test";
trigger.PassEventArgsToCommand = true;
trigger.EventArgsConverter = new TestEventArgsConverter
{
TestPrefix = prefix
};
var vm = new TestViewModel();
var binding = new Binding
{
Source = vm.ParameterCommand
};
var textBox = new TextBox
{
Text = "BoundParameter"
};
var bindingParameter = new Binding
{
Source = textBox,
Path = new PropertyPath("Text")
};
#if SL3
trigger.Command = binding;
trigger.CommandParameter = bindingParameter;
#else
BindingOperations.SetBinding(trigger, EventToCommand.CommandProperty, binding);
BindingOperations.SetBinding(trigger, EventToCommand.CommandParameterProperty, bindingParameter);
#endif
var args = new StringEventArgs("StringEventArgs");
trigger.InvokeWithEventArgs(args);
Assert.IsTrue(vm.CommandExecuted);
Assert.AreEqual(textBox.Text, vm.ParameterReceived);
}
[TestMethod]
public void TestInvokeSimpleCommandWithEventArgsAndConverterAndConverterParameter()
{
var trigger = new EventToCommandStub();
var rectangle = new Rectangle();
((IAttachedObject)trigger).Attach(rectangle);
const string prefix = "Test";
trigger.PassEventArgsToCommand = true;
trigger.EventArgsConverter = new TestEventArgsConverter
{
TestPrefix = prefix
};
trigger.EventArgsConverterParameter = "Suffix";
var vm = new TestViewModel();
var binding = new Binding
{
Source = vm.SimpleCommand
};
#if SL3
trigger.Command = binding;
#else
BindingOperations.SetBinding(trigger, EventToCommand.CommandProperty, binding);
#endif
var args = new StringEventArgs("StringEventArgs");
trigger.InvokeWithEventArgs(args);
Assert.IsTrue(vm.CommandExecuted);
Assert.AreEqual(null, vm.ParameterReceived);
}
[TestMethod]
public void TestInvokeParameterCommandWithEventArgsAndConverterAndConverterParameter()
{
var trigger = new EventToCommandStub();
var rectangle = new Rectangle();
((IAttachedObject)trigger).Attach(rectangle);
const string prefix = "Test";
trigger.PassEventArgsToCommand = true;
trigger.EventArgsConverter = new TestEventArgsConverter
{
TestPrefix = prefix
};
trigger.EventArgsConverterParameter = "Suffix";
var vm = new TestViewModel();
var binding = new Binding
{
Source = vm.ParameterCommand
};
#if SL3
trigger.Command = binding;
#else
BindingOperations.SetBinding(trigger, EventToCommand.CommandProperty, binding);
#endif
var args = new StringEventArgs("StringEventArgs");
trigger.InvokeWithEventArgs(args);
Assert.IsTrue(vm.CommandExecuted);
Assert.AreEqual(prefix + args.Parameter + trigger.EventArgsConverterParameter, vm.ParameterReceived);
}
[TestMethod]
public void TestInvokeWithEventArgsAndNoParameterAndConverterAndConverterParameter()
{
var trigger = new EventToCommandStub();
var rectangle = new Rectangle();
((IAttachedObject)trigger).Attach(rectangle);
const string prefix = "Test";
trigger.PassEventArgsToCommand = true;
trigger.EventArgsConverter = new TestEventArgsConverter
{
TestPrefix = prefix
};
trigger.EventArgsConverterParameter = "Suffix";
var vm = new TestViewModel();
var binding = new Binding
{
Source = vm.CommandWithEventArgsConverted
};
#if SL3
trigger.Command = binding;
#else
BindingOperations.SetBinding(trigger, EventToCommand.CommandProperty, binding);
#endif
var args = new StringEventArgs("StringEventArgs");
trigger.InvokeWithEventArgs(args);
Assert.IsTrue(vm.CommandExecuted);
Assert.AreEqual(prefix + args.Parameter + trigger.EventArgsConverterParameter, vm.ParameterReceived);
}
[TestMethod]
public void TestInvokeWithEventArgsAndParameterValueAndConverterAndConverterParameter()
{
var trigger = new EventToCommandStub();
var rectangle = new Rectangle();
((IAttachedObject)trigger).Attach(rectangle);
const string prefix = "Test";
trigger.PassEventArgsToCommand = true;
trigger.EventArgsConverter = new TestEventArgsConverter
{
TestPrefix = prefix
};
trigger.EventArgsConverterParameter = "Suffix";
var vm = new TestViewModel();
var binding = new Binding
{
Source = vm.ParameterCommand
};
#if SL3
trigger.Command = binding;
#else
BindingOperations.SetBinding(trigger, EventToCommand.CommandProperty, binding);
#endif
const string commandParameter = "CommandParameter";
trigger.CommandParameterValue = commandParameter;
var args = new StringEventArgs("StringEventArgs");
trigger.InvokeWithEventArgs(args);
Assert.IsTrue(vm.CommandExecuted);
Assert.AreEqual(commandParameter, vm.ParameterReceived);
}
[TestMethod]
public void TestInvokeWithEventArgsAndBoundParameterAndConverterAndConverterParameter()
{
var trigger = new EventToCommandStub();
var rectangle = new Rectangle();
((IAttachedObject)trigger).Attach(rectangle);
const string prefix = "Test";
trigger.PassEventArgsToCommand = true;
trigger.EventArgsConverter = new TestEventArgsConverter
{
TestPrefix = prefix
};
trigger.EventArgsConverterParameter = "Suffix";
var vm = new TestViewModel();
var binding = new Binding
{
Source = vm.ParameterCommand
};
var textBox = new TextBox
{
Text = "BoundParameter"
};
var bindingParameter = new Binding
{
Source = textBox,
Path = new PropertyPath("Text")
};
#if SL3
trigger.Command = binding;
trigger.CommandParameter = bindingParameter;
#else
BindingOperations.SetBinding(trigger, EventToCommand.CommandProperty, binding);
BindingOperations.SetBinding(trigger, EventToCommand.CommandParameterProperty, bindingParameter);
#endif
var args = new StringEventArgs("StringEventArgs");
trigger.InvokeWithEventArgs(args);
Assert.IsTrue(vm.CommandExecuted);
Assert.AreEqual(textBox.Text, vm.ParameterReceived);
}
}
}

Просмотреть файл

@ -0,0 +1,13 @@
using System;
using GalaSoft.MvvmLight.Command;
namespace GalaSoft.MvvmLight.Test.Command
{
public class EventToCommandStub : EventToCommand
{
public void InvokeWithEventArgs(EventArgs args)
{
base.Invoke(args);
}
}
}

Просмотреть файл

@ -2,12 +2,9 @@
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Interactivity;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using GalaSoft.MvvmLight.Command;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Windows.Input;
using System;
namespace GalaSoft.MvvmLight.Test.Command
{
@ -410,263 +407,5 @@ namespace GalaSoft.MvvmLight.Test.Command
Assert.IsTrue(vm.CommandExecuted);
#endif
}
[TestMethod]
public void TestInvokeSimpleCommandWithEventArgs()
{
var trigger = new EventToCommandStub();
var rectangle = new Rectangle();
((IAttachedObject)trigger).Attach(rectangle);
trigger.PassEventArgsToCommand = true;
var vm = new TestViewModel();
var binding = new Binding
{
Source = vm.SimpleCommand
};
#if SL3
trigger.Command = binding;
#else
BindingOperations.SetBinding(trigger, EventToCommand.CommandProperty, binding);
#endif
var args = new StringEventArgs("StringEventArgs");
trigger.InvokeWithEventArgs(args);
Assert.IsTrue(vm.CommandExecuted);
Assert.AreEqual(null, vm.ParameterReceived);
}
[TestMethod]
[ExpectedException(typeof(InvalidCastException))]
public void TestInvokeParameterCommandWithEventArgs()
{
var trigger = new EventToCommandStub();
var rectangle = new Rectangle();
((IAttachedObject)trigger).Attach(rectangle);
trigger.PassEventArgsToCommand = true;
var vm = new TestViewModel();
var binding = new Binding
{
Source = vm.ParameterCommand
};
#if SL3
trigger.Command = binding;
#else
BindingOperations.SetBinding(trigger, EventToCommand.CommandProperty, binding);
#endif
var args = new StringEventArgs("StringEventArgs");
trigger.InvokeWithEventArgs(args);
Assert.IsTrue(vm.CommandExecuted);
Assert.AreEqual(args.Parameter, vm.ParameterReceived);
}
[TestMethod]
public void TestInvokeWithEventArgsAndNoParameter()
{
var trigger = new EventToCommandStub();
var rectangle = new Rectangle();
((IAttachedObject)trigger).Attach(rectangle);
trigger.PassEventArgsToCommand = true;
var vm = new TestViewModel();
var binding = new Binding
{
Source = vm.CommandWithEventArgs
};
#if SL3
trigger.Command = binding;
#else
BindingOperations.SetBinding(trigger, EventToCommand.CommandProperty, binding);
#endif
var args = new StringEventArgs("StringEventArgs");
trigger.InvokeWithEventArgs(args);
Assert.IsTrue(vm.CommandExecuted);
Assert.AreEqual(args.Parameter, vm.ParameterReceived);
}
[TestMethod]
public void TestInvokeWithEventArgsAndParameterValue()
{
var trigger = new EventToCommandStub();
var rectangle = new Rectangle();
((IAttachedObject)trigger).Attach(rectangle);
trigger.PassEventArgsToCommand = true;
var vm = new TestViewModel();
var binding = new Binding
{
Source = vm.ParameterCommand
};
#if SL3
trigger.Command = binding;
#else
BindingOperations.SetBinding(trigger, EventToCommand.CommandProperty, binding);
#endif
const string CommandParameter = "CommandParameter";
trigger.CommandParameterValue = CommandParameter;
var args = new StringEventArgs("StringEventArgs");
trigger.InvokeWithEventArgs(args);
Assert.IsTrue(vm.CommandExecuted);
Assert.AreEqual(CommandParameter, vm.ParameterReceived);
}
[TestMethod]
public void TestInvokeWithEventArgsAndBoundParameter()
{
var trigger = new EventToCommandStub();
var rectangle = new Rectangle();
((IAttachedObject)trigger).Attach(rectangle);
trigger.PassEventArgsToCommand = true;
var vm = new TestViewModel();
var binding = new Binding
{
Source = vm.ParameterCommand
};
var textBox = new TextBox
{
Text = "BoundParameter"
};
var bindingParameter = new Binding
{
Source = textBox,
Path = new PropertyPath("Text")
};
#if SL3
trigger.Command = binding;
trigger.CommandParameter = bindingParameter;
#else
BindingOperations.SetBinding(trigger, EventToCommand.CommandProperty, binding);
BindingOperations.SetBinding(trigger, EventToCommand.CommandParameterProperty, bindingParameter);
#endif
var args = new StringEventArgs("StringEventArgs");
trigger.InvokeWithEventArgs(args);
Assert.IsTrue(vm.CommandExecuted);
Assert.AreEqual(textBox.Text, vm.ParameterReceived);
}
private class StringEventArgs : EventArgs
{
public string Parameter
{
get;
private set;
}
public StringEventArgs(string parameter)
{
Parameter = parameter;
}
}
private class EventToCommandStub : EventToCommand
{
public void InvokeWithEventArgs(EventArgs args)
{
base.Invoke(args);
}
}
private class TestViewModel : ViewModelBase
{
public bool CommandExecuted
{
get;
set;
}
public string ParameterReceived
{
get;
private set;
}
public ICommand SimpleCommand
{
get;
private set;
}
public ICommand ParameterCommand
{
get;
private set;
}
public ICommand ToggledCommand
{
get;
private set;
}
public ICommand ToggledCommandWithParameter
{
get;
private set;
}
public readonly RelayCommand<EventArgs> CommandWithEventArgs;
private bool _enableToggledCommand;
public bool EnableToggledCommand
{
private get
{
return _enableToggledCommand;
}
set
{
_enableToggledCommand = value;
((RelayCommand)ToggledCommand).RaiseCanExecuteChanged();
}
}
public TestViewModel()
{
SimpleCommand = new RelayCommand(() => CommandExecuted = true);
ParameterCommand = new RelayCommand<string>(p =>
{
CommandExecuted = true;
ParameterReceived = p;
});
ToggledCommand = new RelayCommand(
() => CommandExecuted = true,
() => EnableToggledCommand);
ToggledCommandWithParameter = new RelayCommand<string>(
p =>
{
CommandExecuted = true;
ParameterReceived = p;
},
p => (p != null && p.StartsWith("Hello")));
CommandWithEventArgs = new RelayCommand<EventArgs>(e =>
{
CommandExecuted = true;
ParameterReceived = ((StringEventArgs)e).Parameter;
});
}
}
}
}

Просмотреть файл

@ -0,0 +1,23 @@
using System;
namespace GalaSoft.MvvmLight.Test.Command
{
public class StringEventArgs : EventArgs
{
public string Parameter
{
get;
private set;
}
public StringEventArgs(string parameter)
{
Parameter = parameter;
}
public override string ToString()
{
return Parameter;
}
}
}

Просмотреть файл

@ -0,0 +1,98 @@
using System;
using System.Windows.Input;
using GalaSoft.MvvmLight.Command;
namespace GalaSoft.MvvmLight.Test.Command
{
public class TestViewModel : ViewModelBase
{
public bool CommandExecuted
{
get;
set;
}
public string ParameterReceived
{
get;
private set;
}
public ICommand SimpleCommand
{
get;
private set;
}
public ICommand ParameterCommand
{
get;
private set;
}
public ICommand ToggledCommand
{
get;
private set;
}
public ICommand ToggledCommandWithParameter
{
get;
private set;
}
public readonly RelayCommand<EventArgs> CommandWithEventArgs;
public readonly RelayCommand<string> CommandWithEventArgsConverted;
private bool _enableToggledCommand;
public bool EnableToggledCommand
{
private get
{
return _enableToggledCommand;
}
set
{
_enableToggledCommand = value;
((RelayCommand)ToggledCommand).RaiseCanExecuteChanged();
}
}
public TestViewModel()
{
SimpleCommand = new RelayCommand(() => CommandExecuted = true);
ParameterCommand = new RelayCommand<string>(p =>
{
CommandExecuted = true;
ParameterReceived = p;
});
ToggledCommand = new RelayCommand(
() => CommandExecuted = true,
() => EnableToggledCommand);
ToggledCommandWithParameter = new RelayCommand<string>(
p =>
{
CommandExecuted = true;
ParameterReceived = p;
},
p => (p != null && p.StartsWith("Hello")));
CommandWithEventArgs = new RelayCommand<EventArgs>(e =>
{
CommandExecuted = true;
ParameterReceived = ((StringEventArgs)e).Parameter;
});
CommandWithEventArgsConverted = new RelayCommand<string>(e =>
{
CommandExecuted = true;
ParameterReceived = e;
});
}
}
}

Просмотреть файл

@ -57,10 +57,15 @@
</CodeAnalysisDependentAssemblyPaths>
</ItemGroup>
<ItemGroup>
<Compile Include="Command\EventToCommandEventArgsTest.cs" />
<Compile Include="Command\EventArgsConverter.cs" />
<Compile Include="Command\EventToCommandStub.cs" />
<Compile Include="Command\EventToCommandTest.cs" />
<Compile Include="Command\RelayCommandGenericTest.cs" />
<Compile Include="Command\RelayCommandTest.cs" />
<Compile Include="Command\StringEventArgs.cs" />
<Compile Include="Command\TemporaryClass.cs" />
<Compile Include="Command\TestViewModel.cs" />
<Compile Include="Helpers\PublicTestClassGenericWithResult.cs" />
<Compile Include="Helpers\InternalTestClassGenericWithResult.cs" />
<Compile Include="Helpers\InternalTestClassWithResult.cs" />

Просмотреть файл

@ -57,6 +57,15 @@
</CodeAnalysisDependentAssemblyPaths>
</ItemGroup>
<ItemGroup>
<Compile Include="..\GalaSoft.MvvmLight.Test %28NET35%29\Command\EventArgsConverter.cs">
<Link>Command\EventArgsConverter.cs</Link>
</Compile>
<Compile Include="..\GalaSoft.MvvmLight.Test %28NET35%29\Command\EventToCommandEventArgsTest.cs">
<Link>Command\EventToCommandEventArgsTest.cs</Link>
</Compile>
<Compile Include="..\GalaSoft.MvvmLight.Test %28NET35%29\Command\EventToCommandStub.cs">
<Link>Command\EventToCommandStub.cs</Link>
</Compile>
<Compile Include="..\GalaSoft.MvvmLight.Test %28NET35%29\Command\EventToCommandTest.cs">
<Link>Command\EventToCommandTest.cs</Link>
</Compile>
@ -66,9 +75,15 @@
<Compile Include="..\GalaSoft.MvvmLight.Test %28NET35%29\Command\RelayCommandTest.cs">
<Link>Command\RelayCommandTest.cs</Link>
</Compile>
<Compile Include="..\GalaSoft.MvvmLight.Test %28NET35%29\Command\StringEventArgs.cs">
<Link>Command\StringEventArgs.cs</Link>
</Compile>
<Compile Include="..\GalaSoft.MvvmLight.Test %28NET35%29\Command\TemporaryClass.cs">
<Link>Command\TemporaryClass.cs</Link>
</Compile>
<Compile Include="..\GalaSoft.MvvmLight.Test %28NET35%29\Command\TestViewModel.cs">
<Link>Command\TestViewModel.cs</Link>
</Compile>
<Compile Include="..\GalaSoft.MvvmLight.Test %28NET35%29\Helpers\InternalTestClassGenericWithResult.cs">
<Link>Helpers\InternalTestClassGenericWithResult.cs</Link>
</Compile>

Просмотреть файл

@ -72,6 +72,15 @@
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Compile Include="..\GalaSoft.MvvmLight.Test %28NET35%29\Command\EventArgsConverter.cs">
<Link>Command\EventArgsConverter.cs</Link>
</Compile>
<Compile Include="..\GalaSoft.MvvmLight.Test %28NET35%29\Command\EventToCommandEventArgsTest.cs">
<Link>Command\EventToCommandEventArgsTest.cs</Link>
</Compile>
<Compile Include="..\GalaSoft.MvvmLight.Test %28NET35%29\Command\EventToCommandStub.cs">
<Link>Command\EventToCommandStub.cs</Link>
</Compile>
<Compile Include="..\GalaSoft.MvvmLight.Test %28NET35%29\Command\EventToCommandTest.cs">
<Link>Command\EventToCommandTest.cs</Link>
</Compile>
@ -81,9 +90,15 @@
<Compile Include="..\GalaSoft.MvvmLight.Test %28NET35%29\Command\RelayCommandTest.cs">
<Link>Command\RelayCommandTest.cs</Link>
</Compile>
<Compile Include="..\GalaSoft.MvvmLight.Test %28NET35%29\Command\StringEventArgs.cs">
<Link>Command\StringEventArgs.cs</Link>
</Compile>
<Compile Include="..\GalaSoft.MvvmLight.Test %28NET35%29\Command\TemporaryClass.cs">
<Link>Command\TemporaryClass.cs</Link>
</Compile>
<Compile Include="..\GalaSoft.MvvmLight.Test %28NET35%29\Command\TestViewModel.cs">
<Link>Command\TestViewModel.cs</Link>
</Compile>
<Compile Include="..\GalaSoft.MvvmLight.Test %28NET35%29\Helpers\CommonTestClass.cs">
<Link>Helpers\CommonTestClass.cs</Link>
</Compile>

Просмотреть файл

@ -95,6 +95,15 @@
<Reference Include="System.Windows.Browser" />
</ItemGroup>
<ItemGroup>
<Compile Include="..\GalaSoft.MvvmLight.Test %28NET35%29\Command\EventArgsConverter.cs">
<Link>Command\EventArgsConverter.cs</Link>
</Compile>
<Compile Include="..\GalaSoft.MvvmLight.Test %28NET35%29\Command\EventToCommandEventArgsTest.cs">
<Link>Command\EventToCommandEventArgsTest.cs</Link>
</Compile>
<Compile Include="..\GalaSoft.MvvmLight.Test %28NET35%29\Command\EventToCommandStub.cs">
<Link>Command\EventToCommandStub.cs</Link>
</Compile>
<Compile Include="..\GalaSoft.MvvmLight.Test %28NET35%29\Command\EventToCommandTest.cs">
<Link>Command\EventToCommandTest.cs</Link>
</Compile>
@ -104,9 +113,15 @@
<Compile Include="..\GalaSoft.MvvmLight.Test %28NET35%29\Command\RelayCommandTest.cs">
<Link>Command\RelayCommandTest.cs</Link>
</Compile>
<Compile Include="..\GalaSoft.MvvmLight.Test %28NET35%29\Command\StringEventArgs.cs">
<Link>Command\StringEventArgs.cs</Link>
</Compile>
<Compile Include="..\GalaSoft.MvvmLight.Test %28NET35%29\Command\TemporaryClass.cs">
<Link>Command\TemporaryClass.cs</Link>
</Compile>
<Compile Include="..\GalaSoft.MvvmLight.Test %28NET35%29\Command\TestViewModel.cs">
<Link>Command\TestViewModel.cs</Link>
</Compile>
<Compile Include="..\GalaSoft.MvvmLight.Test %28NET35%29\Helpers\InternalTestClassGenericWithResult.cs">
<Link>Helpers\InternalTestClassGenericWithResult.cs</Link>
</Compile>

Просмотреть файл

@ -95,6 +95,15 @@
<Reference Include="System.Windows.Browser" />
</ItemGroup>
<ItemGroup>
<Compile Include="..\GalaSoft.MvvmLight.Test %28NET35%29\Command\EventArgsConverter.cs">
<Link>Command\EventArgsConverter.cs</Link>
</Compile>
<Compile Include="..\GalaSoft.MvvmLight.Test %28NET35%29\Command\EventToCommandEventArgsTest.cs">
<Link>Command\EventToCommandEventArgsTest.cs</Link>
</Compile>
<Compile Include="..\GalaSoft.MvvmLight.Test %28NET35%29\Command\EventToCommandStub.cs">
<Link>Command\EventToCommandStub.cs</Link>
</Compile>
<Compile Include="..\GalaSoft.MvvmLight.Test %28NET35%29\Command\EventToCommandTest.cs">
<Link>Command\EventToCommandTest.cs</Link>
</Compile>
@ -104,9 +113,15 @@
<Compile Include="..\GalaSoft.MvvmLight.Test %28NET35%29\Command\RelayCommandTest.cs">
<Link>Command\RelayCommandTest.cs</Link>
</Compile>
<Compile Include="..\GalaSoft.MvvmLight.Test %28NET35%29\Command\StringEventArgs.cs">
<Link>Command\StringEventArgs.cs</Link>
</Compile>
<Compile Include="..\GalaSoft.MvvmLight.Test %28NET35%29\Command\TemporaryClass.cs">
<Link>Command\TemporaryClass.cs</Link>
</Compile>
<Compile Include="..\GalaSoft.MvvmLight.Test %28NET35%29\Command\TestViewModel.cs">
<Link>Command\TestViewModel.cs</Link>
</Compile>
<Compile Include="..\GalaSoft.MvvmLight.Test %28NET35%29\Helpers\InternalTestClassGenericWithResult.cs">
<Link>Helpers\InternalTestClassGenericWithResult.cs</Link>
</Compile>

Просмотреть файл

@ -70,6 +70,15 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="..\GalaSoft.MvvmLight.Test %28NET35%29\Command\EventArgsConverter.cs">
<Link>Command\EventArgsConverter.cs</Link>
</Compile>
<Compile Include="..\GalaSoft.MvvmLight.Test %28NET35%29\Command\EventToCommandEventArgsTest.cs">
<Link>Command\EventToCommandEventArgsTest.cs</Link>
</Compile>
<Compile Include="..\GalaSoft.MvvmLight.Test %28NET35%29\Command\EventToCommandStub.cs">
<Link>Command\EventToCommandStub.cs</Link>
</Compile>
<Compile Include="..\GalaSoft.MvvmLight.Test %28NET35%29\Command\EventToCommandTest.cs">
<Link>Command\EventToCommandTest.cs</Link>
</Compile>
@ -79,9 +88,15 @@
<Compile Include="..\GalaSoft.MvvmLight.Test %28NET35%29\Command\RelayCommandTest.cs">
<Link>Command\RelayCommandTest.cs</Link>
</Compile>
<Compile Include="..\GalaSoft.MvvmLight.Test %28NET35%29\Command\StringEventArgs.cs">
<Link>Command\StringEventArgs.cs</Link>
</Compile>
<Compile Include="..\GalaSoft.MvvmLight.Test %28NET35%29\Command\TemporaryClass.cs">
<Link>Command\TemporaryClass.cs</Link>
</Compile>
<Compile Include="..\GalaSoft.MvvmLight.Test %28NET35%29\Command\TestViewModel.cs">
<Link>Command\TestViewModel.cs</Link>
</Compile>
<Compile Include="..\GalaSoft.MvvmLight.Test %28NET35%29\Helpers\InternalTestClassGenericWithResult.cs">
<Link>Helpers\InternalTestClassGenericWithResult.cs</Link>
</Compile>

Просмотреть файл

@ -90,6 +90,15 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Compile Include="..\GalaSoft.MvvmLight.Test %28NET35%29\Command\EventArgsConverter.cs">
<Link>Command\EventArgsConverter.cs</Link>
</Compile>
<Compile Include="..\GalaSoft.MvvmLight.Test %28NET35%29\Command\EventToCommandEventArgsTest.cs">
<Link>Command\EventToCommandEventArgsTest.cs</Link>
</Compile>
<Compile Include="..\GalaSoft.MvvmLight.Test %28NET35%29\Command\EventToCommandStub.cs">
<Link>Command\EventToCommandStub.cs</Link>
</Compile>
<Compile Include="..\GalaSoft.MvvmLight.Test %28NET35%29\Command\EventToCommandTest.cs">
<Link>Command\EventToCommandTest.cs</Link>
</Compile>
@ -99,9 +108,15 @@
<Compile Include="..\GalaSoft.MvvmLight.Test %28NET35%29\Command\RelayCommandTest.cs">
<Link>Command\RelayCommandTest.cs</Link>
</Compile>
<Compile Include="..\GalaSoft.MvvmLight.Test %28NET35%29\Command\StringEventArgs.cs">
<Link>Command\StringEventArgs.cs</Link>
</Compile>
<Compile Include="..\GalaSoft.MvvmLight.Test %28NET35%29\Command\TemporaryClass.cs">
<Link>Command\TemporaryClass.cs</Link>
</Compile>
<Compile Include="..\GalaSoft.MvvmLight.Test %28NET35%29\Command\TestViewModel.cs">
<Link>Command\TestViewModel.cs</Link>
</Compile>
<Compile Include="..\GalaSoft.MvvmLight.Test %28NET35%29\Helpers\CommonTestClass.cs">
<Link>Helpers\CommonTestClass.cs</Link>
</Compile>
@ -246,9 +261,15 @@
<Compile Include="..\GalaSoft.MvvmLight.Test %28NET35%29\Stubs\ITestClass.cs">
<Link>Stubs\ITestClass.cs</Link>
</Compile>
<Compile Include="..\GalaSoft.MvvmLight.Test %28NET35%29\Stubs\TestBaseClass.cs">
<Link>Stubs\TestBaseClass.cs</Link>
</Compile>
<Compile Include="..\GalaSoft.MvvmLight.Test %28NET35%29\Stubs\TestBindingTarget.cs">
<Link>Stubs\TestBindingTarget.cs</Link>
</Compile>
<Compile Include="..\GalaSoft.MvvmLight.Test %28NET35%29\Stubs\TestChildClass.cs">
<Link>Stubs\TestChildClass.cs</Link>
</Compile>
<Compile Include="..\GalaSoft.MvvmLight.Test %28NET35%29\Stubs\TestClass.cs">
<Link>Stubs\TestClass.cs</Link>
</Compile>
@ -315,7 +336,6 @@
<DesignTime>True</DesignTime>
<DependentUpon>AppResources.resx</DependentUpon>
</Compile>
<Compile Include="Stubs\Class1.cs" />
</ItemGroup>
<ItemGroup>
<ApplicationDefinition Include="App.xaml">

Просмотреть файл

@ -1,21 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using GalaSoft.MvvmLight.Ioc;
namespace GalaSoft.MvvmLight.Test.Stubs
{
public class TestBaseClass
{
public void Remove()
{
SimpleIoc.Default.Unregister(this);
}
}
public class TestChildClass : TestBaseClass
{
}
}