0

我有下面的代码。

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
4

1 回答 1

1

你在哪里调用这个方法?您应该只在构造函数中调用它,并且只调用一次。此外,这是上面代码的更好版本:

this.ObservableForProperty(x => x.SelectedDay)
    .Throttle(TimeSpan.FromMilliseconds(3600), RxApp.DeferredScheduler)
    .Where(x => SelectedDay != null)
    .Subscribe(x => SomeRandomMethod());
于 2013-03-11T16:28:53.827 回答