I have a really large collection, 2000++ items, of images in SVG that users need to filter as they wish. The filtering is done via a text field, so I am doing a simple ObservableCollection
filter with LINQ:
async void SearchChanged(System.Object sender, Xamarin.Forms.TextChangedEventArgs e)
{
SearchableImages.Clear(); // ObservableCollection
if (Search.Text.Length < 3) return;
// ImageCollection is an always in-memory List<ImageItem>
var filter = await Task.FromResult<IEnumerable<ImageItem>>(
ImageCollection.Where((i) => i.Name.Contains(Search.Text.ToLower()))
);
foreach (var p in filter)
SearchableImages.Add(p);
}
This code runs extremely slowly, so much that you can see the if
block there, limiting the length of the search field to 3. Enabling to search characters of any length renders this unusable.
Apparently the Filter
property of a CollectionView is not available:
<CollectionView x:Name="CollectionList" ItemsSource="{Binding SearchableIcons}" ...>
string src = Search.Text.ToLower();
CollectionList.Filter = (item) => item.Name.Contains("hello");
Error CS1061: 'CollectionView' does not contain a definition for 'Filter' and no accessible extension method 'Filter' accepting a first argument of type 'CollectionView' could be found (are you missing a using directive or an assembly reference?) (CS1061)
How can I filter a very large number of images smoothly?