ReactiveUI-5.99.6:ReactiveCommand CanExecute always returns False

ReactiveUI-5.99.6: ReactiveCommand CanExecute always returns False

我正在使用 ReactiveUI-5.99.6,但我无法通过这个简单的测试

public class ViewModel : ReactiveObject
{
    public ReactiveList<int> List { get; private set; }
    public IReactiveCommand Command { get; private set; }

    public ViewModel()
    {
        List = new ReactiveList<int>();
        Command = ReactiveCommand.Create(List.Changed.Select(_ => List.Any()));
    }
}

[TestClass]
public class UnitTest1
{
    [TestMethod]
    public void TestMethod1()
    {
        var vm = new ViewModel();

        vm.List.Add(2);
        Assert.IsTrue(vm.Command.CanExecute(null));
    }
}

有人可以告诉我我做错了什么吗?

我不知道版本 5.99.6 (!?) 但以下实现应该可以使您的单元测试在版本 8.7.2 中通过:

public class ViewModel : ReactiveObject
{
    public ReactiveList<int> List { get; private set; }
    public ICommand Command { get; private set; }

    public ViewModel()
    {
        List = new ReactiveList<int>();
        Command = ReactiveCommand.Create(() => { /* do something */ }, List.Changed.Select(_ => List.Any()));
    }
}

如果您将 Command 属性 的类型更改为 ICommand 以外的任何类型(例如 ReactiveCommand<Unit, Unit>),您的单元测试将如下所示:

[TestMethod]
public async Task TestMethod1()
{
    var vm = new ViewModel();
    vm.List.Add(2);
    Assert.IsTrue(await vm.Command.CanExecute.FirstAsync());
}

What version should i get if my project is .Net 4.5?

ReactiveUI 7.4.0 支持 .NET Framework 4.5。上面的解决方案也适用于此版本。