Block Notifychange unless all the element is added into the observablecollection

This should improve the response speed.
This commit is contained in:
弘韬 张 2020-11-14 12:36:29 +08:00
parent e34a52c74e
commit dab8c5da95

View file

@ -260,7 +260,31 @@ namespace Flow.Launcher.ViewModel
public class ResultCollection : ObservableCollection<ResultViewModel>, INotifyCollectionChanged
{
private bool _suppressNotification = false;
protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
{
if (!_suppressNotification)
base.OnCollectionChanged(e);
}
public override event NotifyCollectionChangedEventHandler CollectionChanged;
// https://peteohanlon.wordpress.com/2008/10/22/bulk-loading-in-observablecollection/
public void AddRange(IEnumerable<ResultViewModel> Items)
{
_suppressNotification = true;
foreach (var item in Items)
{
Add(item);
}
// wpf use directx / double buffered already, so just reset all won't cause ui flickering
CollectionChanged?.Invoke(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
return;
}
public void RemoveAll()
{
ClearItems();
@ -272,20 +296,9 @@ namespace Flow.Launcher.ViewModel
/// <param name="newItems"></param>
public void Update(List<ResultViewModel> newItems)
{
ClearItems();
foreach (var item in newItems)
{
Add(item);
}
// wpf use directx / double buffered already, so just reset all won't cause ui flickering
CollectionChanged?.Invoke(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
return;
AddRange(newItems);
}
}
}