Reputation: 689
I have an AutoSuggestBox and in the textchanged event I set the itemsSource of the Listview each time the text is changed.
var box = sender as AutoSuggestBox;
var searchString = box.Text;
labelFolder.ItemsSource = AllTags.Where(p => p.NAME.Contains(searchString));
Is this good practice? If not, Is there any efficient way to do the same?
Upvotes: 1
Views: 628
Reputation: 1195
If your AllTags
will contains thousands of elements - I suppose it will be inefficient. In addition, probably, TextChanged
event launches after every tap on the keyboard and data reloading is very frequent which could slow your app.
It is a good idea to take a look at the Reactive Extensions in such cases. With Rx you can easily delay data refreshing when user type very fast and select eg. first 20 elements of the Where
result.
var searchObservable = Observable.FromEventPattern(s => box.TextChanged += s, s => box.TextChanged -= s)
.Throttle(TimeSpan.FromMilliseconds(400))
.Select(result =>
{
var textBox = result.Sender as AutoSuggestBox;
return textBox.Text;
}
);
searchObservable
.DistinctUntilChanged()
.ObserveOnDispatcher()
.Subscribe(searchString =>
{
//Select elements from 'AllTags' here, this code will be launched with 400ms delay (throttle) when user is typing fast.
}
Note, this example is very general, but you can base on it.
At first glance, Rx looks difficult, but there are a lot of topics and tutorials about it.
Upvotes: 1