在更新绑定源期间冻结wpf应用程序

在更新绑定源期间冻结wpf应用程序,wpf,listview,binding,freeze,Wpf,Listview,Binding,Freeze,我基于ListView控件创建了缩略图。在ListView.ItemSourceI绑定observedcollection照片{get;set} 我在另一个线程中以并行方式创建缩略图 我简化了我的代码 public class ThumbnailCreator { public static List<Photos> CreateThumbnailImage(List<Photos> photos) { var thumbnails = n

我基于ListView控件创建了缩略图。在
ListView.ItemSource
I绑定
observedcollection照片{get;set}

我在另一个线程中以并行方式创建缩略图

我简化了我的代码

public class ThumbnailCreator
{
    public static List<Photos> CreateThumbnailImage(List<Photos> photos)
    {
        var thumbnails = new List<Photos>();

        Parallel.ForEach(photos, photo =>
        {
            var bitmap = new BitmapImage();

            bitmap.BeginInit();

            bitmap.DecodePixelHeight = 128;

            bitmap.DecodePixelWidth = 128;

            bitmap.CacheOption = BitmapCacheOption.OnLoad;

            bitmap.CreateOptions = BitmapCreateOptions.DelayCreation;

            bitmap.UriSource = new Uri(photo.FullPath);

            bitmap.EndInit();

            if (bitmap.CanFreeze)
                bitmap.Freeze();

           thumbnails.Add(new Photos{ThumbnailImage = bitmap}); 

        });

        return thumbnails;
    }
}
结果-用户界面行为。

正在连续添加图像,但如果集合中包含的图像超过500个,则WPF窗口将冻结。例如,无法移动窗口、滚动列表视图

异步调用

            App.Current.Dispatcher.InvokeAsync(new Action(() =>
            {
                thumbnails.Add(new Photos { ThumbnailImage = bitmap });

            }), DispatcherPriority.ContextIdle);
结果-用户界面行为。

开始时,应用程序会冻结,但几秒钟后,图像会不断添加,也可能会移动窗口,滚动列表视图


所以我的问题是,应用程序冻结的问题根源是什么?我怎样才能避免这种行为

在将项目添加到
可观察集合之前,不要中断绑定

绑定
缩略图。项目来源
照片
OC,然后在另一个线程加载上添加项目到
照片
OC。这样您的UI就不会冻结


您可能希望使用可以在代码项目中找到的多线程ObservableColleciton。搜索ObservableCollectionMt…

我根据你的建议编辑了我的样本,但应用程序仍然冻结。我原来的问题中有更多的细节。
                this.Dispatcher.Invoke(DispatcherPriority.SystemIdle, new Action(() =>
                {
                    Photos = new ObservableCollection<Photos>(thumbnails);

                    this.Thumbnails.ItemsSource = Photos;
                }));
            App.Current.Dispatcher.Invoke(new Action(() =>
            {
                thumbnails.Add(new Photos { ThumbnailImage = bitmap });

            }), DispatcherPriority.ContextIdle);
            App.Current.Dispatcher.InvokeAsync(new Action(() =>
            {
                thumbnails.Add(new Photos { ThumbnailImage = bitmap });

            }), DispatcherPriority.ContextIdle);