xamarin/Xamarin.Forms
[Bug] SearchHandler.ItemsSource does not listen to INotifyCollectionChanged.CollectionChanged event
开放
#6,500 创建于 2019年6月11日
a/shell :shell:e/4 :clock4:help wantedp/iOS 🍎t/bug :bug:up-for-grabs
仓库指标
- 星标
- (5,644 个星标)
- PR 合并指标
- (30 天内没有已合并 PR)
描述
Description
When using a collection implementing INotifyCollectionChanged as a source for SearchHandler.ItemsSource, the results aren't updated when adding/removing/updating the collection.
The UI is updated only when setting a new collection to ItemsSource.
Steps to Reproduce
- Create a solution from the Shell App Template
- Add a new class
CustomSearchHandlerinheriting fromSearchHandler - Implement it as below
- Add this CustomSearchHandler to a page
- Run the app and try to search "bc"
- The results should not be filtered
- Add
ItemsSource = _items.ToList()at the end ofOnQueryChanged - Run the app, search for "bc"
- The results should be filtered
using System.Collections.ObjectModel;
using System.Linq;
using Xamarin.Forms;
namespace TestSearchHandler
{
public class CustomSearchHandler : SearchHandler
{
private static readonly string[] Values = new[]
{
"Abc",
"Bce",
"Xyz",
"Qwe",
"Rer",
"Gep",
"Qdw",
"545"
};
private ObservableCollection<string> _items = new ObservableCollection<string>();
public CustomSearchHandler()
{
ShowsResults = true;
ItemsSource = _items;
}
protected override void OnQueryChanged(string oldValue, string newValue)
{
base.OnQueryChanged(oldValue, newValue);
var newItems = Values.Where(v => v.ToLowerInvariant().Contains(newValue.ToLowerInvariant())).ToList();
for (int i = newItems.Count; i < _items.Count; i++)
{
_items.RemoveAt(i);
}
for (int i = 0; i < newItems.Count; i++)
{
if (i >= _items.Count)
{
_items.Add(newItems[i]);
}
else
{
_items[i] = newItems[i];
}
}
}
}
}
Expected Behavior
Results should be updated when the INotifyCollectionChanged collection is updated.
Actual Behavior
Results are not updated when the INotifyCollectionChanged collection is updated.
Basic Information
- Version with issue: 4.0.0.482894
- IDE: Visual Studio for Mac
- Platform Target Frameworks:
- iOS: 12.1
- Android: 9.0
Screenshots
Actual
Expected
Reproduction Link
https://github.com/TimLariviere/XFIssueRepros/tree/master/SearchHandlerItemsSourceIssue