我有很多存储在独立存储中的图像,并希望将它们显示在列表框中。但是,我不希望所有图像都会立即加载,而是懒洋洋地加载。因此,只有当用户滚动查看新项目时,图像才会被加载。我也想用数据绑定来提供列表项的数据和图像。从隔离存储中缓存加载列表框图像
在测试中,我做的所有图片总是立即加载,所以我不确定是否可以使用默认的ListBox
和数据绑定来实现这种延迟加载。它可以?
我有很多存储在独立存储中的图像,并希望将它们显示在列表框中。但是,我不希望所有图像都会立即加载,而是懒洋洋地加载。因此,只有当用户滚动查看新项目时,图像才会被加载。我也想用数据绑定来提供列表项的数据和图像。从隔离存储中缓存加载列表框图像
在测试中,我做的所有图片总是立即加载,所以我不确定是否可以使用默认的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())。
这很好,谢谢!完美的作品。关键字真的是“数据虚拟化” - 用Google搜索将我带到Shawn Oster的页面,他也详细阐述了该主题:http://shawnoster.com/blog/post/Improving-ListBox-Performance-in-Silverlight- for-Windows-Phone-7-Data-Virtualization.aspx – 2011-05-31 19:22:48
只是为了记录:我没有绑定到IList,我首先绑定到List。用IList替换这个List似乎在做伎俩。 – 2011-05-31 19:31:32
我知道这是永远以前,但使用可观察的集合做同样的事情是吗? – Nico 2012-04-28 23:06:58
检查this LazyListBox实现。该列表框将为屏幕上可见的项目加载复杂的模板。对于屏幕上不可见的项目,您可以设置简单模板。
感谢您的链接,这是非常有趣的阅读。目前Anheledir的解决方案对我来说已经足够了。但是为了将来我会密切关注LazyListBox。 – 2011-05-31 19:23:53
你可以查看这个博客和channel9视频讨论相同。源代码也可用。 http://channel9.msdn.com/Shows/SilverlightTV/Silverlight-TV-72-Windows-Phone-Tips-for-Loading-Images http://jobijoy.blogspot.com/2011/05/wp7dev-tip-2 -few-things-to-remember-on.html – 2011-05-30 10:12:01