我有一个ViewModel,我想监听两个响应式列表(Payments和AccountPayments)中的项目更改。这些列表已被实例化并启用了ChangeTrackingEnabled:
this.Payments = new ReactiveList<transaction.PaymentViewModel>();
this.Payments.ChangeTrackingEnabled = true;`
this.AccountPayments = new ReactiveList<AccountPaymentViewModel>();
this.AccountPayments.ChangeTrackingEnabled = true;`
然后,在我的ViewModel中定义了一个ObservableAsPropertyHelper只读属性:
readonly ObservableAsPropertyHelper<decimal> _totalPayments;
public decimal TotalPayments { get { return _totalPayments.Value; } }
我的意图是在这两个列表中任何一项更改时设置TotalPayments。我尝试使用WhenAny:
this.WhenAny(vm => vm.Payments.ItemChanged,
vm => vm.AccountPayments.ItemChanged,
(a, b) => a.Sender.Payments
.Where(x => x.Amount.HasValue).Sum(x => x.Amount.Value)
+ b.Sender.AccountPayments
.Where(x => x.Amount.HasValue).Sum(x => x.Amount.Value))
.ToProperty(this, vm => vm.TotalPayments, out _totalPayments);
虽然代码编译正常,但似乎无法捕获更改。 我还尝试使用WhenAnyObservable:
this.WhenAnyObservable(
vm => vm.Payments.ItemChanged,
vm => vm.AccountPayments.ItemChanged)
.Select(_ =>
this.Payments.Where(x => x.Amount.HasValue)
.Sum(x => x.Amount.Value)
+ this.AccountPayments.Where(x => x.Amount.HasValue)
.Sum(x => x.Amount.Value))
.ToProperty(this, vm => vm.TotalPayments, out _totalPayments);
但这段代码无法编译通过。 有没有办法实现我想要做的事情?非常感谢您的帮助。
WhenAny
设置了一个属性更改监听器来跟踪其中表达式的变化,因此不支持进行任何花哨的操作。我会更新的。 - Charles Mager