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

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

问题描述

我有一个案例,我有一个 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).

希望这(一些)有意义.

Hopefully this made (some) sense.

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

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 中,这是 ListBoxListView数据网格...).希望这有帮助.

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天全站免登陆