我想爲ListBox使用Microsoft.Windows.APICodePack.Shell.ShellContainer作爲ItemsSource,通過ListBox.ItemTemplate顯示每個孩子的(ShellObject)縮略圖和名稱。 問題出現時ShellContainer指的是一個非常大的文件夾(說一千多個文件):如果我簡單地聲明最好的方法來設置BIG IEnumerable作爲ListBox.ItemSource
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();
這需要比以前的方式更多的時間調用它,但不凍結用戶界面並立即開始顯示一些內容。 有沒有更好的方式來獲得類似的行爲,可能會縮短總的操作時間?
謝謝。虛擬化解決了我的問題,但據我所知它只能通過VirtualizingStackPanel獲得,並且在我的ListBox中我需要一個WrapPanel。我正在調查自己實施一個可行性,但它似乎遠遠超出了我的卑微的技能,而不是談論第三方的選擇:或者他們是非常昂貴的,或者他們似乎是越野車和近似... – italianogrosso