Jer*_*xon 5 c# system.reactive windows-8 winrt-xaml windows-store-apps
Reactive Extensions允许我"观察"一系列事件.例如,当用户在Windows 8搜索窗格中键入其搜索查询时,会反复提出SuggestionsRequested(对于每个字母).如何利用Reactive Extensions来限制请求?
像这样的东西:
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;
};
Run Code Online (Sandbox Code Playgroud)
解
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));
Run Code Online (Sandbox Code Playgroud)
安装Win for WinRT:http://nuget.org/packages/Rx-WinRT/ 了解更多信息:http://blogs.msdn.com/b/rxteam/archive/2012/08/15/reactive-extensions-v2- 0-HAS-arrived.aspx
有Throttle和DistinctUntilChanged方法。
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));
Run Code Online (Sandbox Code Playgroud)
您可能想要/需要使用不同的重载DistinctUntilChanged,例如使用不同的相等比较器或Func<TSource, TKey>重载:
.DistinctUntilChanged(e => e.QueryText.Trim())
Run Code Online (Sandbox Code Playgroud)
那会做你想做的事。
| 归档时间: |
|
| 查看次数: |
733 次 |
| 最近记录: |