Lazy loading of listbox images from isolated storage

前端 未结 2 694
抹茶落季
抹茶落季 2021-01-05 15:46

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 th

相关标签:
2条回答
  • 2021-01-05 16:37

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

    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
    }
    

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

    0 讨论(0)
  • 2021-01-05 16:41

    Check this LazyListBox implementation. This ListBox will load complex template for items visible on screen. For items not visible on screen you set simple template.

    0 讨论(0)
提交回复
热议问题