加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

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

发布时间:2020-12-14 01:48:03 所属栏目:Windows 来源:网络整理
导读:我有很多图像存储在独立存储中,并希望将它们显示在列表框中.但是,我不希望所有图像立即加载,而是懒洋洋地加载.因此,只有当用户滚动查看新项目时才应加载图像.我还想使用数据绑定来提供列表项的数据和图像. 在测试中我做的所有图像总是立即被加载,所以我不确
我有很多图像存储在独立存储中,并希望将它们显示在列表框中.但是,我不希望所有图像立即加载,而是懒洋洋地加载.因此,只有当用户滚动查看新项目时才应加载图像.我还想使用数据绑定来提供列表项的数据和图像.

在测试中我做的所有图像总是立即被加载,所以我不确定是否可以使用默认的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()).

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读