2013-05-16 25 views
5

Reaktif Uzantılar, bir olay akışını "gözlemlememe" olanak sağlar. Örneğin, kullanıcı arama sorgularını Windows 8 Arama Bölmesi'nde yazdığında, Öneriler Sorgulaması her bir harf için yukarı ve yukarı kaldırılır. İstekleri kısmak için Reaktif Uzantıları nasıl kullanabilirim? BöyleSearchPane.SuggestionsRequested'i kısmak için Reactive Extensions'ı nasıl kullanabilirim?

şey:

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; 
}; 

Çözüm

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 için RX yükleyin: http://nuget.org/packages/Rx-WinRT/ daha fazla bilgi: http://blogs.msdn.com/b/rxteam/archive/2012/08/15/reactive-extensions-v2-0-has-arrived.aspx

cevap

4

Throttle ve DistinctUntilChanged yöntem vardır.

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)); 

İstiyorsun/olabilir mesela DistinctUntilChanged için farklı aşırı kullanmak gerekir Ne istediğinizi yapacak

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

: Farklı eşitlik karşılaştırıcısı veya Func<TSource, TKey> aşırı kullanarak.

+0

@ JerryNixon-MSFT Ah, bunun için üzgünüm :) Tarama yaparken patladı ... İyi olsa da haklıymışım. –

İlgili konular