在WPF MVVM中使用ReactiveUI获取属性更改的优先值

Cli*_*int 6 c# wpf reactiveui

我正在使用Reactive UI进行MVVM WPF项目,当属性发生变化时我需要知道:

  1. 变更前的值
  2. 新价值(即变化)

我有一个viewmodel(Deriving from ReactiveObject),其上声明了一个属性:

private AccountHolderType _accountHolderType;
public AccountHolderType AccountHolderType
{
   get { return _accountHolderType; }
   set { this.RaiseAndSetIfChanged(ref _accountHolderType, value); }
}
Run Code Online (Sandbox Code Playgroud)

在构造函数中,我正在尝试执行以下操作:

this.WhenAnyValue(vm => vm.AccountHolderType)
   .Subscribe((old,curr) => { // DO SOMETHING HERE });
Run Code Online (Sandbox Code Playgroud)

但是该WhenAnyValue方法没有这样的过载,并且Reactive文档非常缺乏.

可以访问一个简单的WhenAnyValue:

this.WhenAnyValue(vm => vm.AccountHolderType)
   .Subscribe(val => { // DO SOMETHING HERE });
Run Code Online (Sandbox Code Playgroud)

这让我可以观察更改并获得最新的更改,但我需要访问先前的值.

我知道我可以将它作为一个简单的属性来实现,这样:

public AccountHolderType AccountHolderType
{
   get { // }
   set
   {
      var prev = _accountHolderType;
      _accountHolderType = value;

      // Do the work with the old and new value
      DoSomething(prev, value);
   }
}
Run Code Online (Sandbox Code Playgroud)

但鉴于该项目正在使用Reactive UI,我希望尽可能地"反应性".

Kur*_*oro 10

这个怎么样:

 this.WhenAnyValue(vm => vm.AccountHolderType)
      .Buffer(2, 1)
      .Select(b => new { Previous = b[0], Current = b[1] })
      .Subscribe(t => { 
//Logic using previous and new value for AccountHolderType 
});
Run Code Online (Sandbox Code Playgroud)

我想你已经错过了这个直接缓冲功能.