从独立存储延迟加载列表框图像 [英] Lazy loading of listbox images from isolated storage

查看:13
本文介绍了从独立存储延迟加载列表框图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多图像存储在独立存储中,并希望将它们显示在列表框中.但是,我不希望立即加载所有图像而是懒惰地加载.因此,只有当用户滚动查看新项目时,才应加载图像.我还想使用数据绑定来提供列表项的数据和图像.

I have lots of images stored in isolated storage and want to display them in a listbox. However, I don't want all images to be loaded right away but lazily. So only when the user scrolls to see new items the images should be loaded. I also want to use data binding for providing the list item's data and image.

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

In the tests I did all images always got loaded right away and at once, so I am unsure whether this lazy loading can be achieved with the default ListBox and data binding. Can it?

推荐答案

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

You can use the standard ListBox to "lazy load" your items with databinding. The keyword here is "data virtualization". You have to implement IList to the class you want to databind. The indexer method will only be called for the items currently visible and for the next calculated ~2 screens. This is also the reason you should use a fixed size grid for your item-layout, not a stackpanel with a calculated height based on all containing items (performance!).

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

You don't have to implement all IList members, just a few. Here is an example:

    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()).

While debugging you can see that not all items are loaded at once but only when needed (see Debug.WriteLine()).

这篇关于从独立存储延迟加载列表框图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆