From a4147f52c93d8ecd5af31f1b7e8e5130ff26693d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=98=E9=9F=AC=20=E5=BC=A0?= Date: Sat, 14 Nov 2020 12:36:29 +0800 Subject: [PATCH] Block Notifychange unless all the element is added into the observablecollection This should improve the response speed. --- Flow.Launcher/ViewModel/ResultsViewModel.cs | 36 ++++++++++++++------- 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/Flow.Launcher/ViewModel/ResultsViewModel.cs b/Flow.Launcher/ViewModel/ResultsViewModel.cs index d62e0d0c3..6210dc54f 100644 --- a/Flow.Launcher/ViewModel/ResultsViewModel.cs +++ b/Flow.Launcher/ViewModel/ResultsViewModel.cs @@ -260,7 +260,30 @@ namespace Flow.Launcher.ViewModel public class ResultCollection : ObservableCollection, INotifyCollectionChanged { + private bool _suppressNotification = false; + protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e) + { + if (!_suppressNotification) + base.OnCollectionChanged(e); + } + public override event NotifyCollectionChangedEventHandler CollectionChanged; + + public void AddRange(IEnumerable 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 +295,9 @@ namespace Flow.Launcher.ViewModel /// public void Update(List 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); } } }