2013-05-16 12 views
5

リアクティブエクステンションを使用すると、一連のイベントを「監視」することができます。たとえば、ユーザーがWindows 8の検索ペインで検索クエリを入力しているときは、SuggestionsRequestedが何度も繰り返されます(それぞれの文字について)。リアクティブエクステンションを利用してリクエストを抑制するにはどうすればよいですか?このようなReactive Extensionsを使用してSearchPane.SuggestionsRequestedを抑制するにはどうすればよいですか?

何か:

SearchPane.GetForCurrentView().SuggestionsRequested += (s, e) => 
{ 
    if (e.QueryText.Length < 3) 
     return; 
    // TODO: if identical to the last request, return; 
    // TODO: if asked less than 500ms ago, return; 
}; 

ソリューション

System.Reactive.Linq.Observable.FromEventPattern<Windows.ApplicationModel.Search.SearchPaneSuggestionsRequestedEventArgs> 
    (Windows.ApplicationModel.Search.SearchPane.GetForCurrentView(), "SuggestionsRequested") 
    .Throttle(TimeSpan.FromMilliseconds(500), System.Reactive.Concurrency.Scheduler.CurrentThread) 
    .Where(x => x.EventArgs.QueryText.Length > 3) 
    .DistinctUntilChanged(x => x.EventArgs.QueryText.Trim()) 
    .Subscribe(x => HandleSuggestions(x.EventArgs)); 

WinRTのためにRXをインストールします。http://nuget.org/packages/Rx-WinRT/ はこちらをご覧ください:http://blogs.msdn.com/b/rxteam/archive/2012/08/15/reactive-extensions-v2-0-has-arrived.aspx

答えて

4

ThrottleDistinctUntilChangedの方法があります。

System.Reactive.Linq.Observable.FromEventPattern<Windows.ApplicationModel.Search.SearchPaneSuggestionsRequestedEventArgs> 
    (Windows.ApplicationModel.Search.SearchPane.GetForCurrentView(), "SuggestionsRequested") 
    .Throttle(TimeSpan.FromMilliseconds(500), System.Reactive.Concurrency.Scheduler.CurrentThread) 
    .Where(x => x.EventArgs.QueryText.Length > 3) 
    .DistinctUntilChanged(x => x.EventArgs.QueryText.Trim()) 
    .Subscribe(x => HandleSuggestions(x.EventArgs)); 

DistinctUntilChangedに異なるオーバーロードを使用する必要があります。あなたがやりたいだろう

.DistinctUntilChanged(e => e.QueryText.Trim()) 

:異なる等値比較やFunc<TSource, TKey>オーバーロードを使用しました。

+0

@ JerryNixon-MSFTああ、申し訳ありませんが、私がブラウズしていたときにポップアップしました...うれしかったです。 –