Reputation: 89
Simple case:
public class Foo : ReactiveObject
{
public Foo()
{
this.ObservableForProperty(t => t.Bar, t => t).Subscribe(t =>
{
//Logic using previous and new value for Bar
}
}
private int _bar;
public int Bar
{
get { return _bar; }
set { this.RaiseAndSetIfChanged(ref _bar, value); }
}
}
In the ObservableForProperty subscription, only the new value of Bar is accessible (via t). We can call ObservableForProperty with true for the "beforeChange" parameter, and instead only have the previous value and not the new.
I know I can just plug my code logic in the property setter, but I'd like to keep the ObservableForProperty behavior (filter the first setter and when the value does not change). The property is also used in a XAML binding and require the propertyChanged trigger.
Anything I missed? How can I do that easily? Thanks
Upvotes: 0
Views: 898
Reputation: 1962
How about something like this:
public Foo()
{
this.WhenAnyValue(t => t.Bar)
.Buffer(2, 1)
.Select(buf => new { Previous = buf[0], Current = buf[1] })
.Subscribe(t => { //Logic using previous and new value for Bar });
}
Note that this will not trigger the subscription logic the first time you alter the Bar. In order to get that functionality, add .StartWith(this.Bar)
before buffering.
This uses Buffer operator in overlapping mode (skip
< count
).
Upvotes: 4