我有 2 个 DecimalUpDown 控件,num_one 和 num_two,分别绑定到属性 First 和 Second。当 First 更改时,它将联系服务器以计算 Second 的值,反之亦然。异步触发服务器调用会释放 UI,但是在快速触发(例如滚轮)时,最后一个请求并不总是最后一个返回,因此值可能会变得不同步。
使用 Reactive 我试图限制调用以仅在用户停止更改一段时间后触发服务器调用。问题是当您在更新期间进行更改时,属性更改开始相互触发并根据油门的时间跨度来回卡住。
public MainWindow()
{
InitializeComponent();
DataContext = this;
Observable.FromEventPattern<RoutedPropertyChangedEventHandler<object>, RoutedPropertyChangedEventArgs<object>>(h => num_one.ValueChanged += h, h => num_one.ValueChanged -= h)
.Throttle(TimeSpan.FromMilliseconds(100), Scheduler.ThreadPool)
.Subscribe(x =>
{
Thread.Sleep(300); // simulate work
Second = (decimal)x.EventArgs.NewValue / 3.0m;
});
Observable.FromEventPattern<RoutedPropertyChangedEventHandler<object>, RoutedPropertyChangedEventArgs<object>>(h => num_two.ValueChanged += h, h => num_two.ValueChanged -= h)
.Throttle(TimeSpan.FromMilliseconds(100), Scheduler.ThreadPool)
.Subscribe(x =>
{
Thread.Sleep(300); // simulate work
First = (decimal)x.EventArgs.NewValue * 3.0m;
});
}
private decimal first;
public decimal First
{
get { return first; }
set
{
first = value;
NotifyPropertyChanged("First");
}
}
private decimal second;
public decimal Second
{
get { return second; }
set
{
second = value;
NotifyPropertyChanged("Second");
}
}