我有很多图像存储在独立存储中,并希望将它们显示在列表框中。但是,我不希望立即加载所有图像,而是延迟加载。因此,只有当用户滚动查看新项目时,才应加载图像。我还想使用数据绑定来提供列表项的数据和图像。
在测试中,我所做的所有图像总是立即立即加载,所以我不确定是否可以使用默认值实现这种延迟加载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())。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)