如何在不使用分页的情况下实现延迟加载的Silverlight数据网格 [英] How to implement a lazy loaded Silverlight data grid without using paging

查看:113
本文介绍了如何在不使用分页的情况下实现延迟加载的Silverlight数据网格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用来自全新发布的RIA Services的业务应用程序模板,您可以在DomainDataSource顶部与DataPager结合使用数据网格来查看许多示例. PageSize和LoadSize属性可用于调整要在一页中显示的数据量以及在后台预取的数据.

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.

现在,我想拥有一个带有滚动条且没有分页器的数据网格.基础DomainDataSource应该仅加载在网格中显示的数据.当用户向下滚动到不在数据上下文中的项目时,它应该触发另一个负载.是否有任何示例实现该如何实现?

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?

推荐答案

我刚刚发布了几篇博客文章(第1部分第2部分)提供了解决此问题的方法.我还将一个示例发布到GitHub,该示例实现了我对VirtualCollection概念的自己的看法(我不知道这与Infragistics的控件相比如何,因为我没有使用它.)

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

为了展示使用的简便性,以下是示例中的一些片段.首先,这是您使用 VirtualCollection 的方式坐标获取数据:

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();
    }
}

在XAML中,您只需将Items属性绑定到ListBoxDataGrid

In XAML you simply bind the Items property to the ItemsSource property of a ListBox or DataGrid

对于每个数据源,您必须实现VirtualCollectionSource.以下是 NetflixTitlesSource 的两种关键方法:

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屋!

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