如何在不使用分页的情况下实现延迟加载的 Silverlight 数据网格 [英] How to implement a lazy loaded Silverlight data grid without using paging
问题描述
使用来自全新发布的 RIA 服务的业务应用程序模板,您可以看到许多使用 DomainDataSource
顶部的数据网格结合 DataPager
的示例.PageSize 和 LoadSize 属性可用于调整一页显示的数据量和后台预取的数据量.
现在我想要一个带有滚动条但没有分页器的数据网格.底层 DomainDataSource
应该只加载在网格中显示的数据.当用户向下滚动到尚未出现在数据上下文中的项目时,它应该触发另一个加载.是否有任何示例实现如何执行此操作?
我刚刚发表了几篇博文 (第 1 部分,
为了展示它的易用性,以下是示例中的一些片段.首先,这是您如何使用 VirtualCollection 的类坐标获取数据:
公共类 MainViewModel : ViewModel{私人 NetflixTitlesSource _source;public VirtualCollection项目{得到;私人订制;}公共主视图模型(){_source = 新的 NetflixTitlesSource();Items = new VirtualCollection(_source, pageSize: 20, cachedPages: 5);}受保护的覆盖 void OnViewLoaded(){Items.Refresh();}}
在 XAML 中,您只需将 Items
属性绑定到 ListBox
或 DataGrid
的 ItemsSource
属性>
对于每个数据源,您必须实现一个 VirtualCollectionSource.下面是 NetflixTitlesSource 的两个关键方法:
公共类 NetflixTitlesSource : VirtualCollectionSource{受保护的覆盖任务获取计数(){返回 GetQueryResults(0, 1, null).ContinueWith(t => (int)t.Result.TotalCount, TaskContinuationOptions.ExecuteSynchronously);}protected override Task>GetPageAsyncOverride(int start, int pageSize, IList sortDescriptions){返回 GetQueryResults(start, pageSize, sortDescriptions).ContinueWith(t => (IList)((IEnumerable)t.Result).ToList(), TaskContinuationOptions.ExecuteSynchronously);}private Task>GetQueryResults(int start, int pageSize, IList sortDescriptions){//查询 Netflix OData API 的代码}}
Using the Business Application template from the brand new released RIA Services, you can see lots of examples using the data grid on top of a DomainDataSource
in combination with a DataPager
. The properties PageSize and LoadSize can be used to adjust the amount of data to be displayed in one page and the data that is prefetched in the background.
Now I'd like to have a data grid with a scrollbar and no pager. The underlying DomainDataSource
should load only the data that is diplayed in the grid. It should trigger another load, when the user scrolls down to items that are not yet in the data context. Is there any sample implementation how to do this?
I've just published a couple of blog posts (Part 1, Part 2) that give my solution to this problem. I have also posted a sample to GitHub that implements my own take on the VirtualCollection concept (I don't know how this compares with Infragistics's control, because I haven't used that).
To show how easy it is to use, here are a few snippets from the sample. First, here's how you use VirtualCollection, the class that coordinates fetching the data:
public class MainViewModel : ViewModel
{
private NetflixTitlesSource _source;
public VirtualCollection<Title> Items { get; private set; }
public MainViewModel()
{
_source = new NetflixTitlesSource();
Items = new VirtualCollection<Title>(_source, pageSize: 20, cachedPages: 5);
}
protected override void OnViewLoaded()
{
Items.Refresh();
}
}
In XAML you simply bind the Items
property to the ItemsSource
property of a ListBox
or DataGrid
For each data source you must implement a VirtualCollectionSource. Here's what the two key methods of NetflixTitlesSource look like:
public class NetflixTitlesSource : VirtualCollectionSource<Title>
{
protected override Task<int> GetCount()
{
return GetQueryResults(0, 1, null)
.ContinueWith(t => (int)t.Result.TotalCount, TaskContinuationOptions.ExecuteSynchronously);
}
protected override Task<IList<Title>> GetPageAsyncOverride(int start, int pageSize, IList<SortDescription> sortDescriptions)
{
return GetQueryResults(start, pageSize, sortDescriptions)
.ContinueWith(t => (IList<Title>)((IEnumerable<Title>)t.Result).ToList(), TaskContinuationOptions.ExecuteSynchronously);
}
private Task<QueryOperationResponse<Title>> GetQueryResults(int start, int pageSize, IList<SortDescription> sortDescriptions)
{
// code to query the Netflix OData API
}
}
这篇关于如何在不使用分页的情况下实现延迟加载的 Silverlight 数据网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!