c# - RxUI ObservableAsPropertyHelper 不适用于 XAML 绑定(bind)

标签 c# wpf xaml binding reactiveui

我有以下 View 模型类(基于 RxUI design guidelines ):

public class SomeViewModel : ReactiveObject
{
    private readonly ObservableAsPropertyHelper<int> m_count;

    public ReactiveCommand<object> AddItem { get; set; }

    public int Count
    {
        get { return m_count.Value; }
    }

    public IReactiveList<string> SomeList { get; private set; }

    public SomeViewModel ()
    {
        SomeList = new ReactiveList<string>();

        AddItem = ReactiveCommand.Create();
        AddItem.Subscribe(x => SomeList.Add("new item"));

        m_count = SomeList.CountChanged.ToProperty(this, x => x.Count, 100);

        SomeList.Add("first item");
    }
}

以及以下 XAML 绑定(bind):

<Button Command="{Binding AddItem}" Content="{Binding Count}" />

当我的 View 显示时,按钮内容是 100 而不是 1。然后当单击按钮时,内容更新为 2,然后是 3,然后是 4,等等。

为什么没有观察到对 SomeList.Add() 的第一次调用?

最佳答案

这是一个微妙的错误。 ToProperty 是惰性,这意味着它不会开始订阅,直到设置 XAML 绑定(bind)(即 调用 SomeList 之后)。您应该可以通过以下方式解决此问题:

 SomeList.CountChanged.StartWith(SomeList.Count).ToProperty(this, x => x.Count, 100);

关于c# - RxUI ObservableAsPropertyHelper 不适用于 XAML 绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28204529/

相关文章:

c# - 在绑定(bind)中使用 StringFormat 显示带空格的十六进制值

c# - Kafka 消费者启动延迟融合 dotnet

c# - 使用 LINQ 插入数据库

c# - 为什么默认情况下 Switch 语句的 Fall-Through?

c# - 提供应用内购买的试用

.net - 拉伸(stretch) WrapPanel 项目

c# - 当所有异步方法调用完成时引发事件

c# - WPF DataGrid 的 EnableRowVirtualization 和 VirtualizingStackPanel.IsVirtualizing 属性之间的区别

c# - 另一个进程使用的文件c#

wpf - WPF MVVM设计问题