我有很多图像存储在独立存储中,并希望将它们显示在列表框中.但是,我不希望所有图像立即加载,而是懒洋洋地加载.因此,只有当用户滚动查看新项目时才应加载图像.我还想使用数据绑定来提供列表项的数据和图像.
在测试中我做的所有图像总是立即被加载,所以我不确定是否可以使用默认的ListBox和数据绑定实现这种延迟加载.它可以?
您可以使用标准ListBox通过数据绑定“延迟加载”您的项目.这里的关键字是“数据虚拟化”.您必须将IList实现到您想要数据绑定的类.仅对当前可见的项目和下一个计算的~2个屏幕调用索引器方法.这也是您应该为项目布局使用固定大小网格的原因,而不是基于所有包含项目的计算高度的堆栈面板(性能!).
您不必实现所有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()).