ReactiveCommand从不挂接canExecute observable的事件处理程序

时间:2015-11-13 13:36:44

标签: system.reactive reactiveui

在我的测试用例中,我可以验证事件处理程序CollectionChanged是否正确连接。创建命令时会发生这种情况。在我的视图模型中,当我做同样的事情时,事件处理程序永远不会被连接起来。这是为什么?

如果我在视图模型中对Undo.CanExecute(null)进行显式调用,它将挂钩事件处理程序。我想我不应该这样做,而且我的视图模型代码肯定有问题。

查看模型

ActionManager = new ActionManager();    

var canUndo = Observable
    .FromEventPattern(e => ActionManager.CollectionChanged += e, e => ActionManager.CollectionChanged -= e)
    .Select(_ => ActionManager.CanUndo)
    ;

Undo = ReactiveCommand.CreateAsyncTask(canUndo, UndoAsync);

测试用例

public class MiscTests
{
    [Fact]
    public void CanExecute()
    { 
        var am = new ActionManager();
        var canUndo = Observable
            .FromEventPattern(e => am.CollectionChanged += e, e => am.CollectionChanged -= e)
            .Select(_ => am.CanUndo);

        var command = ReactiveCommand.Create(canUndo);

        var action = new CallMethodAction(() => { }, () => { });
        var canExecute = command.CanExecute(null);
        canExecute.Should().BeFalse();
        am.Execute(action);
        canExecute = command.CanExecute(null);
        canExecute.Should().BeTrue();

    }
}

0 个答案:

没有答案