Browse Source

Use TypeUtilities to convert the type of CommandParameter to the correct type.

pull/1179/head
Jeremy Koritzinsky 9 years ago
parent
commit
83ee601735
  1. 14
      src/Markup/Avalonia.Markup/AlwaysEnabledDelegateCommand.cs
  2. 3
      tests/Avalonia.Markup.Xaml.UnitTests/Data/BindingTests_Method.cs

14
src/Markup/Avalonia.Markup/AlwaysEnabledDelegateCommand.cs

@ -1,5 +1,8 @@
using System; using Avalonia.Utilities;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Globalization;
using System.Reflection;
using System.Text; using System.Text;
using System.Windows.Input; using System.Windows.Input;
@ -9,9 +12,13 @@ namespace Avalonia.Markup
{ {
private readonly Delegate action; private readonly Delegate action;
private ParameterInfo parameterInfo;
public AlwaysEnabledDelegateCommand(Delegate action) public AlwaysEnabledDelegateCommand(Delegate action)
{ {
this.action = action; this.action = action;
var parameters = action.Method.GetParameters();
parameterInfo = parameters.Length == 0 ? null : parameters[0];
} }
#pragma warning disable 0067 #pragma warning disable 0067
@ -22,13 +29,14 @@ namespace Avalonia.Markup
public void Execute(object parameter) public void Execute(object parameter)
{ {
if (action.Method.GetParameters().Length == 0) if (parameterInfo == null)
{ {
action.DynamicInvoke(); action.DynamicInvoke();
} }
else else
{ {
action.DynamicInvoke(parameter); TypeUtilities.TryConvert(parameterInfo.ParameterType, parameter, CultureInfo.CurrentCulture, out object convertedParameter);
action.DynamicInvoke(convertedParameter);
} }
} }
} }

3
tests/Avalonia.Markup.Xaml.UnitTests/Data/BindingTests_Method.cs

@ -61,8 +61,7 @@ namespace Avalonia.Markup.Xaml.UnitTests.Data
Assert.Equal("Called 5", vm.Value); Assert.Equal("Called 5", vm.Value);
} }
} }
// Should this work or should it be a binding error?
[Fact] [Fact]
public void Binding_Method_To_TextBlock_Text_Works() public void Binding_Method_To_TextBlock_Text_Works()
{ {

Loading…
Cancel
Save