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

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

问题描述

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

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实现到您想要数据绑定的类。仅对当前可见的项目和下一个计算的~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天全站免登陆