我有以下代码。
this.ObservableForProperty(x => x.SelectedDay)
.Throttle(TimeSpan.FromMilliseconds(3600))
.Where(x => SelectedDay != null)
.ObserveOn(CoreWindow.GetForCurrentThread().Dispatcher)
.Subscribe(x => SomeRandomMethod());
Throttle运行良好,在x.SelectedDay停止更改之前不会调用SomeRandomMethod。然而,它每次改变时都会调用它。
So i do this:
Change,
Change,
Wait
//SomeRandomMethod called 2 times at end of throttle
Change
Wait
//SomeRandomMethod called 3 times
Change
Change
Change
Wait
//SomeRandomMethod called 6 times.
我怎样才能让它忽略所有以前的变化事件,并且只在油门完成时才获得最新的事件。
So i would like this:
Change
Change
Wait
//SomeRandomMethod called once
Change
Wait
//SomeRandomMethod called once
Change
Change
Change
Wait
//SomeRandomMethod called once
答案 0 :(得分:1)
你在哪里叫这种方法?您应该只在构造函数中调用它,并且只调用它一次。此外,这是上面代码的更好版本:
this.ObservableForProperty(x => x.SelectedDay)
.Throttle(TimeSpan.FromMilliseconds(3600), RxApp.DeferredScheduler)
.Where(x => SelectedDay != null)
.Subscribe(x => SomeRandomMethod());