具有多个ReactiveCommand IsExecuting的单个ObservableAsPropertyHelper

时间:2015-09-15 14:23:56

标签: c# .net wpf system.reactive reactiveui

从示例here,我正在尝试将_SpinnerVisibility更改为多个reactivecommand对象。所以下面的片段显示了我做了什么..

public ReactiveCommand<object> ExecuteSearch { get; protected set; }

public ReactiveCommand<List<string>> OtherOperation { get; protected set; }

public AppViewModel(ReactiveCommand<object> testExecuteSearchCommand = null, IObservable<List<FlickrPhoto>> testSearchResults = null)
{

_SpinnerVisibility = OtherOperation.IsExecuting.Select(x=> x? Visibility.Visible : Visibility.Collapsed)                
                .ToProperty(this, x => x.SpinnerVisibility, Visibility.Hidden);
_SpinnerVisibility = ExecuteSearch.IsExecuting.Select(x=> x? Visibility.Visible : Visibility.Collapsed)                
                .ToProperty(this, x => x.SpinnerVisibility, Visibility.Hidden);
}

我在这里根据多个反应命令设置可见性。但总是最后注册的命令会触发可见性。有没有办法根据反应命令ExecuteSearch, OtherOperation

更改可见性

1 个答案:

答案 0 :(得分:2)

您需要将两个IsExecuting observable合并为一个。

如果您的命令是互斥的(也就是说它们不能同时执行),这应该有效:

_SpinnerVisibility = new IReactiveCommand[] { ExecuteSearch, OtherOperation }
    .Select(cmd => cmd.IsExecuting)
    .Merge()
    .Select(x=> x? Visibility.Visible : Visibility.Collapsed)                
    .ToProperty(this, x => x.SpinnerVisibility, Visibility.Hidden);

如果可以的话,那就更像是:

_SpinnerVisibility = new IReactiveCommand[] { ExecuteSearch, OtherOperation }
    .Select(cmd => cmd.IsExecuting)
    .CombineLatest()
    .Select(x => x.Any() ? Visibility.Collapsed : Visibility.Visible)                
    .ToProperty(this, x => x.SpinnerVisibility, Visibility.Hidden);

两者都适用于任意数量的命令。