diff --git a/src/Uno.Extensions.Reactive.Generator/Bindables/MappedMembers/CommandFromMethod.CommandConfigGenerator.cs b/src/Uno.Extensions.Reactive.Generator/Bindables/MappedMembers/CommandFromMethod.CommandConfigGenerator.cs index beebdd2a84..c41b8866a4 100644 --- a/src/Uno.Extensions.Reactive.Generator/Bindables/MappedMembers/CommandFromMethod.CommandConfigGenerator.cs +++ b/src/Uno.Extensions.Reactive.Generator/Bindables/MappedMembers/CommandFromMethod.CommandConfigGenerator.cs @@ -42,9 +42,13 @@ public override string ToString() { if (ParameterType is not null && CanExecute is not null) { - sb.AppendLine($@" - if (reactive_commandParameter is not {ParameterType} reactive_arguments - || !({CanExecute("reactive_arguments")})) + sb.AppendLine($@"if (!(reactive_commandParameter is {ParameterType})) + {{ + return false; + }} + + var reactive_arguments = ({ParameterType}) reactive_commandParameter; + if (!({CanExecute("reactive_arguments")})) {{ return false; }}".Align(0)); @@ -52,7 +56,7 @@ public override string ToString() } else if (ParameterType is not null) { - sb.AppendLine($@"if (reactive_commandParameter is not {ParameterType}) + sb.AppendLine($@"if (!(reactive_commandParameter is {ParameterType})) {{ return false; }}".Align(0)); diff --git a/src/Uno.Extensions.Reactive.Generator/Bindables/MappedMembers/CommandFromMethod.cs b/src/Uno.Extensions.Reactive.Generator/Bindables/MappedMembers/CommandFromMethod.cs index 87809bed52..0bf59559c6 100644 --- a/src/Uno.Extensions.Reactive.Generator/Bindables/MappedMembers/CommandFromMethod.cs +++ b/src/Uno.Extensions.Reactive.Generator/Bindables/MappedMembers/CommandFromMethod.cs @@ -167,7 +167,7 @@ string GetTypeOrTuple(IEnumerable parameters) { if (parameters.Count() is 1) { - if (parameters.First().Symbol.NullableAnnotation is NullableAnnotation.NotAnnotated) + if (parameters.First().Symbol is { Type.IsValueType: false, NullableAnnotation: NullableAnnotation.NotAnnotated }) { return args => $"{args} is not null"; } @@ -177,7 +177,9 @@ string GetTypeOrTuple(IEnumerable parameters) if (parameters.Any(p => p.Symbol.NullableAnnotation is NullableAnnotation.NotAnnotated)) { return args => parameters - .Select((p, i) => p.Symbol.NullableAnnotation is NullableAnnotation.NotAnnotated ? $"{args}.Item{i} is not null" : null) + .Select((p, i) => p.Symbol is { Type.IsValueType: false, NullableAnnotation: NullableAnnotation.NotAnnotated } + ? $"{args}.Item{i} is not null" + : null) .Where(s => s is not null) .JoinBy(" && "); } diff --git a/src/Uno.Extensions.Reactive.Tests/Generator/Given_Methods_Then_GenerateCommands.cs b/src/Uno.Extensions.Reactive.Tests/Generator/Given_Methods_Then_GenerateCommands.cs index 8f4fe9e85d..aa3d406d70 100644 --- a/src/Uno.Extensions.Reactive.Tests/Generator/Given_Methods_Then_GenerateCommands.cs +++ b/src/Uno.Extensions.Reactive.Tests/Generator/Given_Methods_Then_GenerateCommands.cs @@ -63,6 +63,56 @@ public async Task When_OneParameter_Void() vm.InvokeCount.Should().Be(1); } + public partial class When_OneValueTypeParameter_Void_ViewModel + { + public int InvokeCount { get; set; } + + public DateTimeOffset? LastInvokeParameter { get; set; } + + public void MyMethod(DateTimeOffset parameter) + { + LastInvokeParameter = parameter; + InvokeCount++; + } + } + + [TestMethod] + public async Task When_OneValueTypeParameter_Void() + { + var vm = new When_OneValueTypeParameter_Void_ViewModel.BindableWhen_OneValueTypeParameter_Void_ViewModel(); + + vm.MyMethod.Execute(new DateTimeOffset(1983, 9, 9, 15, 00, 00, TimeSpan.FromHours(1))); + await WaitFor(() => vm.InvokeCount == 1); + + vm.LastInvokeParameter.Should().Be(new DateTimeOffset(1983, 9, 9, 15, 00, 00, TimeSpan.FromHours(1))); + vm.InvokeCount.Should().Be(1); + } + + public partial class When_OneNullableValueTypeParameter_Void_ViewModel + { + public int InvokeCount { get; set; } + + public DateTimeOffset? LastInvokeParameter { get; set; } + + public void MyMethod(DateTimeOffset? parameter) + { + LastInvokeParameter = parameter; + InvokeCount++; + } + } + + [TestMethod] + public async Task When_OneNullableValueTypeParameter_Void() + { + var vm = new When_OneNullableValueTypeParameter_Void_ViewModel.BindableWhen_OneNullableValueTypeParameter_Void_ViewModel(); + + vm.MyMethod.Execute(new DateTimeOffset(1983, 9, 9, 15, 00, 00, TimeSpan.FromHours(1))); + await WaitFor(() => vm.InvokeCount == 1); + + vm.LastInvokeParameter.Should().Be(new DateTimeOffset(1983, 9, 9, 15, 00, 00, TimeSpan.FromHours(1))); + vm.InvokeCount.Should().Be(1); + } + public partial class When_OneParameterAndCT_Void_ViewModel { public int InvokeCount { get; set; }