|
8 | 8 | using System.Reflection;
|
9 | 9 | using System.Threading;
|
10 | 10 | using System.Threading.Tasks;
|
| 11 | +using System.Windows.Input; |
11 | 12 | using CommunityToolkit.Mvvm.ComponentModel;
|
12 | 13 | using CommunityToolkit.Mvvm.Input;
|
13 | 14 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
@@ -554,6 +555,17 @@ static void AssertOptionsOfT<T>(IAsyncRelayCommand<T> command, AsyncRelayCommand
|
554 | 555 | AssertOptionsOfT(model.OfTAndAllowConcurrentExecutionsAndFlowExceptionsToTaskSchedulerCommand, AsyncRelayCommandOptions.AllowConcurrentExecutions | AsyncRelayCommandOptions.FlowExceptionsToTaskScheduler);
|
555 | 556 | }
|
556 | 557 |
|
| 558 | + // See https://github.com/CommunityToolkit/dotnet/issues/294 |
| 559 | + [TestMethod] |
| 560 | + public void Test_RelayCommandAttribute_CanExecuteWithNullabilityAnnotations() |
| 561 | + { |
| 562 | + ModelWithCommandWithNullableCanExecute model = new(); |
| 563 | + |
| 564 | + Assert.IsTrue(model.DoSomething1Command.CanExecute("Hello")); |
| 565 | + Assert.IsTrue(model.DoSomething2Command.CanExecute("Hello")); |
| 566 | + Assert.IsTrue(model.DoSomething3Command.CanExecute((0, "Hello"))); |
| 567 | + } |
| 568 | + |
557 | 569 | #region Region
|
558 | 570 | public class Region
|
559 | 571 | {
|
@@ -994,4 +1006,37 @@ private Task OfTAndAllowConcurrentExecutionsAndFlowExceptionsToTaskScheduler(str
|
994 | 1006 | return Task.CompletedTask;
|
995 | 1007 | }
|
996 | 1008 | }
|
| 1009 | + |
| 1010 | + partial class ModelWithCommandWithNullableCanExecute |
| 1011 | + { |
| 1012 | + bool CanDoSomething1(string? parameter) |
| 1013 | + { |
| 1014 | + return !string.IsNullOrEmpty(parameter); |
| 1015 | + } |
| 1016 | + |
| 1017 | + [RelayCommand(CanExecute = (nameof(CanDoSomething1)))] |
| 1018 | + private void DoSomething1(string? parameter) |
| 1019 | + { |
| 1020 | + } |
| 1021 | + |
| 1022 | + bool CanDoSomething2(string parameter) |
| 1023 | + { |
| 1024 | + return !string.IsNullOrEmpty(parameter); |
| 1025 | + } |
| 1026 | + |
| 1027 | + [RelayCommand(CanExecute = (nameof(CanDoSomething2)))] |
| 1028 | + private void DoSomething2(string? parameter) |
| 1029 | + { |
| 1030 | + } |
| 1031 | + |
| 1032 | + bool CanDoSomething3((int A, string? B) parameter) |
| 1033 | + { |
| 1034 | + return !string.IsNullOrEmpty(parameter.B); |
| 1035 | + } |
| 1036 | + |
| 1037 | + [RelayCommand(CanExecute = (nameof(CanDoSomething3)))] |
| 1038 | + private void DoSomething3((int A, string? B) parameter) |
| 1039 | + { |
| 1040 | + } |
| 1041 | + } |
997 | 1042 | }
|
0 commit comments