c# - 将 BIG IEnumerable 设置为 ListBox.ItemSsource 的最佳方法

标签 c# wpf listbox windows-api-code-pack

我正在尝试使用 Microsoft.Windows.APICodePack.Shell.ShellContainer 作为 ListBox 的 ItemsSource,通过 ListBox.ItemTemplate 显示每个 child 的 (ShellObject) 缩略图和名称。 当 ShellContainer 引用一个非常大的文件夹(比如超过一千个文件)时,问题就出现了:如果我简单地声明

    ShellContainer source=ShellObject.FromParsingName(@"C:\MyFolder") as ShellContainer:
    listBox1.ItemsSource=source.GetEnumerator();

它会卡住 UI 两三分钟,然后立即显示 ShellContainer 的所有内容。 我发现的最佳解决方法是创建一个像这样的异步填充类

class AsyncSourceFiller
{
    private ObservableCollection<ShellObject> source;
    private ShellContainer path;
    private Control parent;
    private ShellObject item;

    public AsyncSourceFiller(ObservableCollection<ShellObject> source, ShellContainer path, Control parent)
    {
        this.source = source;
        this.path = path;
        this.parent = parent;
    }

    public void Fill()
    {
        foreach (ShellObject obj in path)
        {
            item = obj;
            parent.Dispatcher.Invoke(new Action(Add));
            Thread.Sleep(4);
        }
    }

    private void Add()
    {
        source.Add(item);
    }
}

然后通过

调用它
    ObservableCollection<ShellObject> source = new ObservableCollection<ShellObject>();
    listBox1.ItemsSource = source;
    ShellContainer path = ShellObject.FromParsingName(@"C:\MyFolder"):
    AsyncSourceFiller filler = new AsyncSourceFiller(source, path, this);
    Thread th = new Thread(filler.Fill);
    th.IsBackground = true;
    th.Start();

这比以前的方式需要更多时间,但不会卡住 UI 并立即开始显示一些内容。 有没有更好的方法来获得类似的行为,可能会缩短总操作时间?

最佳答案

在后台线程中加载所有数据,并在完成后更新列表框的项目源。并且不要忘记在您的列表框中将 Virtualization 设置为 true。

关于c# - 将 BIG IEnumerable 设置为 ListBox.ItemSsource 的最佳方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10478075/

相关文章:

c# - 在 WPF 中将 Canvas 转换为可写位图的最快方法?

vba - 从列表框写入后刷新工作表

c# - 项目具有多个可选区域的列表框

c# - 如何有效地包装固定大小的循环缓冲区的索引

c# - Open/SaveFileDialog 类之间的区别及其在 WPF 窗体中的使用

c# - slider /轨迹栏替代方案

c# - Caliburn Micro 取消窗口从 ViewModel 关闭

c# - WPF ListBox 布局(同时考虑所有行的动态列宽)

java - 使用 java 将增强 for 循环转换为 c# 时出错

c# - 从 Dynamics CRM API 返回什么 DateTime