我有以下 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/