gpt4 book ai didi

c# - ReactiveUI:R/W 属性与输出属性

转载 作者:行者123 更新时间:2023-12-03 10:35:16 25 4
gpt4 key购买 nike

我有一个“关闭”按钮和一个 Expander在我的 MVVM View 中链接如下:

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

如果用户点击按钮,展开器应该被折叠。在 View 模型中,我有一个 ReactiveCommand 应该处理这个问题:
public ReactiveCommand<object> CloseResults { get; protected set; } =
ReactiveCommand.Create();

在 View 模型中, HasExecuted是一个输出属性,应该根据其值展开/折叠扩展器:
private readonly ObservableAsPropertyHelper<bool> _hasExecuted;
public bool HasExecuted => _hasExecuted.Value;

因此,要将命令与按钮 Hook ,我将绑定(bind) HasExecuted像这样的命令:
CloseResults.Select(_ => false).ToProperty(this, vm => vm.HasExecuted, out _hasExecuted);

这似乎没有任何作用。但是,如果我改用读写属性并像这样连接它:
CloseResults.Subscribe(_ => { HasExecuted = false; });

它完美地工作。谁能解释为什么输出属性在这种情况下不起作用?是不是 ToProperty应该订阅 IOberservable<bool> 的扩展程序那 Select(_ => false)回来了吗?

我仍然在掌握这一切的窍门,所以我可能缺少一些明显的东西。

最佳答案

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

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

^ 这就是说“无论它给出什么 CloseResults 作为输出,都返回一个始终返回 false 的 Observable”

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

^ 这就是说“将始终为假的 Observable 转化为 HasExecuted 输出属性。”

您的读/写属性更适合您在此处尝试执行的操作。

关于c# - ReactiveUI:R/W 属性与输出属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33053633/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com