从孤立存储中延迟加载列表框图像

前端之家收集整理的这篇文章主要介绍了从孤立存储中延迟加载列表框图像前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有很多图像存储在独立存储中,并希望将它们显示在列表框中.但是,我不希望所有图像立即加载,而是懒洋洋地加载.因此,只有当用户滚动查看新项目时才应加载图像.我还想使用数据绑定来提供列表项的数据和图像.

在测试中我做的所有图像总是立即被加载,所以我不确定是否可以使用默认的ListBox和数据绑定实现这种延迟加载.它可以?

您可以使用标准ListBox通过数据绑定“延迟加载”您的项目.这里的关键字是“数据虚拟化”.您必须将IList实现到您想要数据绑定的类.仅对当前可见的项目和下一个计算的~2个屏幕调用索引器方法.这也是您应该为项目布局使用固定大小网格的原因,而不是基于所有包含项目的计算高度的堆栈面板(性能!).

您不必实现所有IList成员,只需几个.这是一个例子:

  1. public class MyVirtualList : IList {
  2. private List<string> tmpList;
  3.  
  4. public MyVirtualList(List<string> mydata) {
  5. tmpList = new List<string>();
  6. if (mydata == null || mydata.Count <= 0) return;
  7. foreach (var item in mydata)
  8. tmpList.Add(item);
  9. }
  10.  
  11. public int Count {
  12. get { return tmpList != null ? tmpList.Count : 0; }
  13. }
  14.  
  15. public object this[int index] {
  16. get {
  17. Debug.WriteLine("Just requested item #" + index);
  18. return tmpList[index];
  19. }
  20. set {
  21. throw new NotImplementedException();
  22. }
  23. }
  24.  
  25. public int IndexOf(object value) {
  26. return tmpList.IndexOf(value as string);
  27. }
  28.  
  29. public int Add(object value) {
  30. tmpList.Add(value as string);
  31. return Count - 1;
  32. }
  33.  
  34. #region not implemented methods
  35. public void Clear() {
  36. throw new NotImplementedException();
  37. }
  38.  
  39. public bool Contains(object value) {
  40. throw new NotImplementedException();
  41. }
  42.  
  43. public void Insert(int index,object value) {
  44. throw new NotImplementedException();
  45. }
  46.  
  47. public bool IsFixedSize {
  48. get { throw new NotImplementedException(); }
  49. }
  50.  
  51. public bool IsReadOnly {
  52. get { throw new NotImplementedException(); }
  53. }
  54.  
  55. public void Remove(object value) {
  56. throw new NotImplementedException();
  57. }
  58.  
  59. public void RemoveAt(int index) {
  60. throw new NotImplementedException();
  61. }
  62.  
  63. public void CopyTo(Array array,int index) {
  64. throw new NotImplementedException();
  65. }
  66.  
  67. public bool IsSynchronized {
  68. get { throw new NotImplementedException(); }
  69. }
  70.  
  71. public object SyncRoot {
  72. get { throw new NotImplementedException(); }
  73. }
  74.  
  75. public IEnumerator GetEnumerator() {
  76. throw new NotImplementedException();
  77. }
  78. #endregion
  79. }

在调试时,您可以看到并非所有项目都是一次加载,而是仅在需要时加载(请参阅Debug.WriteLine()).

猜你在找的Windows相关文章