如何异步加载可观察集合列表

本文关键字:观察 集合 列表 加载 何异步 异步 | 更新日期: 2023-09-27 18:27:00

我正在应用程序中加载图像。显示图像需要一些时间。

我收集项目作为可观察的收集列表,其中图像存储在独立存储中。

我想异步获取和显示图像?

如何异步加载可观察集合列表

尝试

public class ObservableCollectionThreadSafe<T> : ObservableCollection<T>
{
    // Override the event so this class can access it
    public override event NotifyCollectionChangedEventHandler CollectionChanged;
    public ObservableCollectionThreadSafe()
    {
    }
    public ObservableCollectionThreadSafe(IEnumerable<T> items)
        : base(items)
    {
    }
    protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
    {
        // Be nice - use BlockReentrancy like MSDN said
        using (BlockReentrancy())
        {
            NotifyCollectionChangedEventHandler eventHandler = CollectionChanged;
            if (eventHandler == null)
                return;
            Delegate[] delegates = eventHandler.GetInvocationList();
            // Walk thru invocation list
            foreach (NotifyCollectionChangedEventHandler handler in delegates)
            {
                DispatcherObject dispatcherObject = handler.Target as DispatcherObject;
                // If the subscriber is a DispatcherObject and different thread
                if (dispatcherObject != null && dispatcherObject.CheckAccess() == false)
                {
                    // Invoke handler in the target dispatcher's thread
                    dispatcherObject.Dispatcher.Invoke(DispatcherPriority.DataBind, handler, this, e);
                }
                else // Execute handler as is
                    handler(this, e);
            }
        }
    }
}

编辑:顺便说一句,这不是我的代码,是有人在网上找到的。。。所以"某人"如果你认出了自己,你应该为此而受到赞扬…