1

I have a requirement in my wp7 application that I have to do partial match searching (multiple fields say firstname and lastname) on a list collection (say contact list) on every keypress on search text box and bind to listbox. I have written linq query to get the result on texchanged event. I am getting the result as expected, however it is not quick in response if I have more than 500 items in the collection.

I have posted the code below, I really appreciate if someone can help me out in tunning the performance issue.

private void TechChanged(object sender, TextChangedEventArgs e)
{
    IList<Contacts> results = searchcontList
            .Where(tb => tb.SearchText.Contains(textsearch.Text))
            .ToList();

    //"SearchText" is an attribute in contacts class which is concatination values of all the fields in contacts class
    listcontact.ItemsSource = results;
}
4

1 回答 1

3

您很可能无法提高搜索性能(即查找匹配项并将其呈现到 UI 所花费的时间) - 因此您将需要考虑更改应用程序的工作方式以使其实现感觉反应更灵敏。

您真的需要对输入的每个TextBox字符执行搜索吗?

您的问题的一个常见解决方案是一个称为“节流”的概念,您仅在用户在其文本输入中暂停一段时间时才执行搜索。您可以使用 Reactive Extensions 轻松完成此操作,如下所示:

Observable.FromEvent<TextChangedEventArgs>(searchTextBox, "TextChanged")
          .Select(e => ((TextBox)e.Sender).Text)            
          .Where(text => text.Length > 2)
          .Throttle(TimeSpan.FromMilliseconds(400))         
          .Subscribe(txt => // do your search here!);   

上面的代码确保在您开始搜索之前有两个以上的字符,并限制以确保每 400 毫秒只执行一次搜索。有关详细信息,请参阅我写的 codeproject 文章:

通过 Twitter 和 Bing 地图混搭探索反应式扩展 (Rx)

于 2012-05-03T07:46:40.260 回答