在测试中我做的所有图像总是立即被加载,所以我不确定是否可以使用默认的ListBox和数据绑定实现这种延迟加载.它可以?
在测试中我做的所有图像总是立即被加载,所以我不确定是否可以使用默认的ListBox和数据绑定实现这种延迟加载.它可以?
您不必实现所有IList成员,只需几个.这是一个例子:
public class MyVirtualList : IList { private List<string> tmpList; public MyVirtualList(List<string> mydata) { tmpList = new List<string>(); if (mydata == null || mydata.Count <= 0) return; foreach (var item in mydata) tmpList.Add(item); } public int Count { get { return tmpList != null ? tmpList.Count : 0; } } public object this[int index] { get { Debug.WriteLine("Just requested item #" + index); return tmpList[index]; } set { throw new NotImplementedException(); } } public int IndexOf(object value) { return tmpList.IndexOf(value as string); } public int Add(object value) { tmpList.Add(value as string); return Count - 1; } #region not implemented methods public void Clear() { throw new NotImplementedException(); } public bool Contains(object value) { throw new NotImplementedException(); } public void Insert(int index,object value) { throw new NotImplementedException(); } public bool IsFixedSize { get { throw new NotImplementedException(); } } public bool IsReadOnly { get { throw new NotImplementedException(); } } public void Remove(object value) { throw new NotImplementedException(); } public void RemoveAt(int index) { throw new NotImplementedException(); } public void CopyTo(Array array,int index) { throw new NotImplementedException(); } public bool IsSynchronized { get { throw new NotImplementedException(); } } public object SyncRoot { get { throw new NotImplementedException(); } } public IEnumerator GetEnumerator() { throw new NotImplementedException(); } #endregion }
在调试时,您可以看到并非所有项目都是一次加载,而是仅在需要时加载(请参阅Debug.WriteLine()).