延迟加载不可见元素 [英] Lazy loading of non-visible elements

查看:44
本文介绍了延迟加载不可见元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到的情况是,我有一个 gridview / listbox /任何类型的项目控件,并且绑定到该控件的项目数量很大(很容易达到5000+标记).

I have a case where I have either a gridview/listbox/any type of items control and the number of items bound to the control is massive (easily around 5000+ mark).

每个项目都需要具有从各种Web服务加载的各种属性.显然,接触到Web服务以一次处理所有数量的元素是不可能的.

Each of these items needs to have various attributes loaded from various web services. Obviously, reaching out to web services to process this amount of elements all at once is out of the question.

我的问题是,是否可以推迟加载,直到这些项目实际显示给用户?像这样,用户向下滚动,尽管这些项目一直存在于集合中,但只有在实际渲染它们时才对其进行处理.

My question is, is it possible to postpone loading until these items are actually displayed to the user? As in, the user scrolls down and although the items have been present in the collection all along, they are processed only when they are actually physically rendered.

我以前看过它,但是我不记得确切在哪里.在这种情况下,有很多股票报价绑定到gridview的集合中,但是它们的属性(价格等)为空,直到它们第一次显示(滚动到其各自的位置).

I've seen it done before, but I can't remember where exactly. It was a situation where lots of stock quotes were in a collection bound to a gridview, but their attributes (prices etc...) were empty until they were displayed for the first time (by scrolling to their respective position).

希望这很有道理.

关于如何实现它的任何想法?

Any ideas on how to pull it off?

推荐答案

我会尝试延迟加载和异步加载的组合:
使用虚拟化列表控件.为您的项目创建一个ViewModel并用ViewModel的实例填充您的列表(每行一个).

I would try a combination of lazy loading and asynchronous loading:
Use a virtualizing list-control. Create a ViewModel for your items and fill your list with instances of the ViewModel (one per line).

在ViewModel中,使属性具有默认值,该默认值向用户显示尚未加载数据.第一次访问这些属性之一时,触发异步加载数据,并在收到实际数据时触发 INotifyPropertyChanged .

In your ViewModel, make properties that have a default-value that shows the user that the data not has been loaded. The first time one of these property is accessed, trigger loading the data asynchronous and fire INotifyPropertyChanged when the real data has been received.

这将为用户带来良好的体验,并且大多数棘手的工作将通过虚拟化列表完成(在WPF中,这是 ListBox ListView DataGrid ...).希望这会有所帮助.

This will give the user a nice experience and most of the tricky work will be done through the virtualizing list (in WPF this are ListBox,ListView, DataGrid...). Hope this helped.

class LineItemVM : INotifyPropertyChanged{

  bool   m_loadingTriggered;
  string m_name="Loading...";
  string m_anotherProperty="Loading...";


  public string Name{
     get{
       TriggerLoadIfNecessary(); // Checks if data must be loaded
       return m_name;
     }
  }

  public string AnotherProperty{
     get{
       TriggerLoadIfNecessary(); // Checks if data must be loaded
       return m_anotherProperty;
     }
  }


  void TriggerLoadIfNecessary(){        
     if(!m_loadingTriggered){
       m_loadingTriggered=true;

       // This block will called before your item will be displayed
       //  Due to the m_loadingTriggered-member it is called only once.
       // Start here the asynchronous loading of the data
       // In virtualizing lists, this block is only called if the item
       //  will be visible to the user (he scrolls to this item)

       LoadAsync();
     }
  }

  ...

其他逻辑作为一个想法,您还可以创建一个外部异步加载线程,该线程在后台加载所有数据,但是具有应以更高优先级加载的项目的列表.这个概念与上面的示例相同,但是 TriggerLoadIfNecessary 方法不是从ViewModel-item加载数据,而是仅将该项添加到高优先级列表中,以便潜在的可见元素首先加载.哪个版本更合适的问题取决于列表的用法.如果用户可能使用了完整列表并且没有快速浏览,则此扩展版本会更好.否则,原始版本可能会更好.

Additional logic As an idea, you could also make an outer asynchrounous loading thread that loads all data in background, but has a list for items that should be loaded with higher priority. The concept is the same as in the above example, but instead of loading data from your ViewModel-item, the TriggerLoadIfNecessary-method only adds the item in the high-priority list so that the potentially visible elements are loaded first. The question which version is better suited depends on the usage of the list. If it is probable that the user uses the full list and does not navigate quickly away, this extended version is better. Otherwise the original version is probably better.

这篇关于延迟加载不可见元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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