ReactiveUI:R/W 属性与输出属性

ReactiveUI: R/W properties vs. Output Properties

我的 MVVM 视图中有一个 "Close" 按钮和一个 Expander 链接,如下所示:

this.BindCommand(ViewModel, vm => vm.CloseResults, v => v.CloseButton);
this.OneWayBind(ViewModel, vm => vm.HasExecuted, v => v.Panel.IsExpanded);

如果用户点击按钮,展开器应该折叠起来。在视图模型中,我有一个 ReactiveCommand 应该处理这个问题:

public ReactiveCommand<object> CloseResults { get; protected set; } =
   ReactiveCommand.Create();

在视图模型中,HasExecuted 是一个输出 属性,它应该 expand/collapse 扩展器,具体取决于它的值:

private readonly ObservableAsPropertyHelper<bool> _hasExecuted;
public bool HasExecuted => _hasExecuted.Value;

所以为了将命令与按钮挂钩,我将 HasExecuted 绑定到命令,如下所示:

CloseResults.Select(_ => false).ToProperty(this, vm => vm.HasExecuted, out _hasExecuted);

这似乎没有任何作用。但是,如果我改用读写 属性 并像这样连接它:

CloseResults.Subscribe(_ => { HasExecuted = false; });

它工作得很好。谁能解释为什么 Output 属性 在这种情况下不起作用? ToProperty 扩展不是应该订阅 Select(_ => false) 返回的 IOberservable<bool> 吗?

我还在掌握这一切的窍门,所以我可能遗漏了一些明显的东西。

输出属性旨在反映其他属性或可观察对象的状态。它基本上是您编写的一个小公式,它给出了 属性 作为输出。您不应该直接设置它们。参见 the docs for this

CloseResults.Select(_ => false).ToProperty(this, vm => vm.HasExecuted, out _hasExecuted);

^ 这就是说 "No matter what CloseResults it giving as an output, return an Observable that always returns false"

CloseResults.Select(_ => false).ToProperty(this, vm => vm.HasExecuted, out _hasExecuted);

^ 这是说 "Take that always-false Observable and turn it into the HasExecuted output property."

你的 read/write 属性 更适合你在这里尝试做的事情。