[源码下载]

重新想象 Windows 8 Store Apps (54) - 绑定: 增量方式加载数据

作者:webabcd

介绍
重新想象 Windows 8 Store Apps 之 绑定

  • 通过实现 ISupportIncrementalLoading 接口,为 ListViewBase 的增量加载提供数据

示例
实现 ISupportIncrementalLoading 接口,以便为 ListViewBase 的增量加载提供数据
Binding/MyIncrementalLoading.cs

/*
* 演示如何实现 ISupportIncrementalLoading 接口,以便为 ListViewBase 的增量加载提供数据
*
* ISupportIncrementalLoading - 用于支持增量加载
* HasMoreItems - 是否还有更多的数据
* IAsyncOperation<LoadMoreItemsResult> LoadMoreItemsAsync(uint count) - 异步加载指定数量的数据(增量加载)
*
* LoadMoreItemsResult - 增量加载的结果
* Count - 实际已加载的数据量
*/ using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.UI.Core;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Data; namespace XamlDemo.Binding
{
public class MyIncrementalLoading<T> : ObservableCollection<T>, ISupportIncrementalLoading
{
// 是否正在异步加载中
private bool _isBusy = false; // 提供数据的 Func
// 第一个参数:增量加载的起始索引;第二个参数:需要获取的数据量;第三个参数:获取到的数据集合
private Func<int, int, List<T>> _funcGetData;
// 最大可显示的数据量
private uint _totalCount = ; /// <summary>
/// 构造函数
/// </summary>
/// <param name="totalCount">最大可显示的数据量</param>
/// <param name="getDataFunc">提供数据的 Func</param>
public MyIncrementalLoading(uint totalCount, Func<int, int, List<T>> getDataFunc)
{
_funcGetData = getDataFunc;
_totalCount = totalCount;
} /// <summary>
/// 是否还有更多的数据
/// </summary>
public bool HasMoreItems
{
get { return this.Count < _totalCount; }
} /// <summary>
/// 异步加载数据(增量加载)
/// </summary>
/// <param name="count">需要加载的数据量</param>
/// <returns></returns>
public IAsyncOperation<LoadMoreItemsResult> LoadMoreItemsAsync(uint count)
{
if (_isBusy)
{
throw new InvalidOperationException("忙着呢,先不搭理你");
}
_isBusy = true; var dispatcher = Window.Current.Dispatcher; return AsyncInfo.Run(
(token) =>
Task.Run<LoadMoreItemsResult>(
async () =>
{
try
{
// 模拟长时任务
await Task.Delay(); // 增量加载的起始索引
var startIndex = this.Count; await dispatcher.RunAsync(
CoreDispatcherPriority.Normal,
() =>
{
// 通过 Func 获取增量数据
var items = _funcGetData(startIndex, (int)count);
foreach (var item in items)
{
this.Add(item);
}
}); // Count - 实际已加载的数据量
return new LoadMoreItemsResult { Count = (uint)this.Count };
}
finally
{
_isBusy = false;
}
},
token));
}
}
}

演示如何实现 ListViewBase 的增量加载
Binding/IncrementalLoading.xaml

<Page
x:Class="XamlDemo.Binding.IncrementalLoading"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:XamlDemo.Binding"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"> <Grid Background="Transparent">
<Grid Margin="120 0 0 10"> <TextBlock Name="lblMsg" FontSize="14.667" /> <ListView x:Name="listView" Width="300" Height="300" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="0 30 0 0">
<ListView.ItemTemplate>
<DataTemplate>
<Border Background="Blue" Width="200" CornerRadius="3" HorizontalAlignment="Left">
<TextBlock Text="{Binding Name}" FontSize="14.667" />
</Border>
</DataTemplate>
</ListView.ItemTemplate>
</ListView> <TextBlock Name="lblLog" FontSize="14.667" Margin="0 350 0 0" />
</Grid>
</Grid>
</Page>

Binding/IncrementalLoading.xaml.cs

/*
* 演示如何实现 ListViewBase 的增量加载
* 数据源需要实现 ISupportIncrementalLoading 接口,详见:MyIncrementalLoading.cs
*
*
* ListViewBase - ListView 和 GridView 均继承自 ListViewBase
* IncrementalLoadingTrigger - 增量加载的触发方式(IncrementalLoadingTrigger 枚举)
* Edge - 允许触发增量加载,默认值
* None - 禁止触发增量加载
* DataFetchSize - 预提数据的大小,默认值 3.0
* 本例将此值设置为 4.0 ,其效果为(注:本例中的 ListView 每页可显示的数据量为 6 条或 7 条,以下计算需基于此)
* 1、先获取 1 条数据,为的是尽量快地显示数据
* 2、再获取 4.0 * 1 条数据
* 3、再获取 4.0 * (6 或 7,如果 ListView 当前显示了 6 条数据则为 6,如果 ListView 当前显示了 7 条数据则为 7) 条数据
* 4、以后每次到达阈值后,均增量加载 4.0 * (6 或 7,如果 ListView 当前显示了 6 条数据则为 6,如果 ListView 当前显示了 7 条数据则为 7) 条数据
* IncrementalLoadingThreshold - 阈值,默认值 0.0
* 本例将此值设置为 2.0 ,其效果为(注:本例中的 ListView 每页可显示的数据量为 6 条或 7 条)
* 1、滚动中,如果已准备好的数据少于 2.0 * (6 或 7,如果 ListView 当前显示了 6 条数据则为 6,如果 ListView 当前显示了 7 条数据则为 7) 条数据,则开始增量加载
*/ using Windows.UI.Xaml.Controls;
using XamlDemo.Model;
using System.Linq;
using System.Collections.Specialized;
using System; namespace XamlDemo.Binding
{
public sealed partial class IncrementalLoading : Page
{
// 实现了增量加载的数据源
private MyIncrementalLoading<Employee> _employees; public IncrementalLoading()
{
this.InitializeComponent(); this.Loaded += IncrementalLoading_Loaded;
} void IncrementalLoading_Loaded(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
listView.IncrementalLoadingTrigger = IncrementalLoadingTrigger.Edge;
listView.DataFetchSize = 4.0;
listView.IncrementalLoadingThreshold = 2.0; _employees = new MyIncrementalLoading<Employee>(, (startIndex, count) =>
{
lblLog.Text += string.Format("从索引 {0} 处开始获取 {1} 条数据", startIndex, count);
lblLog.Text += Environment.NewLine; return TestData.GetEmployees().Skip(startIndex).Take(count).ToList();
}); _employees.CollectionChanged += _employees_CollectionChanged; listView.ItemsSource = _employees;
} void _employees_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
lblMsg.Text = "已获取的数据量:" + _employees.Count.ToString();
}
}
}

OK
[源码下载]

重新想象 Windows 8 Store Apps (54) - 绑定: 增量方式加载数据的更多相关文章

  1. 重新想象 Windows 8 Store Apps (55) - 绑定: MVVM 模式

    [源码下载] 重新想象 Windows 8 Store Apps (55) - 绑定: MVVM 模式 作者:webabcd 介绍重新想象 Windows 8 Store Apps 之 绑定 通过 M ...

  2. 重新想象 Windows 8 Store Apps (52) - 绑定: 与 Element Model Indexer Style RelativeSource 绑定, 以及绑定中的数据转换

    [源码下载] 重新想象 Windows 8 Store Apps (52) - 绑定: 与 Element Model Indexer Style RelativeSource 绑定, 以及绑定中的数 ...

  3. 重新想象 Windows 8 Store Apps (53) - 绑定: 与 ObservableCollection CollectionViewSource VirtualizedFilesVector VirtualizedItemsVector 绑定

    [源码下载] 重新想象 Windows 8 Store Apps (53) - 绑定: 与 ObservableCollection CollectionViewSource VirtualizedF ...

  4. 重新想象 Windows 8 Store Apps 系列文章索引

    [源码下载][重新想象 Windows 8.1 Store Apps 系列文章] 重新想象 Windows 8 Store Apps 系列文章索引 作者:webabcd 1.重新想象 Windows ...

  5. 重新想象 Windows 8 Store Apps (23) - 文件系统: 文本的读写, 二进制的读写, 流的读写, 最近访问列表和未来访问列表

    原文:重新想象 Windows 8 Store Apps (23) - 文件系统: 文本的读写, 二进制的读写, 流的读写, 最近访问列表和未来访问列表 [源码下载] 重新想象 Windows 8 S ...

  6. 重新想象 Windows 8 Store Apps (59) - 锁屏

    [源码下载] 重新想象 Windows 8 Store Apps (59) - 锁屏 作者:webabcd 介绍重新想象 Windows 8 Store Apps 之 锁屏 登录锁屏,获取当前程序的锁 ...

  7. 重新想象 Windows 8 Store Apps (15) - 控件 UI: 字体继承, Style, ControlTemplate, SystemResource, VisualState, VisualStateManager

    原文:重新想象 Windows 8 Store Apps (15) - 控件 UI: 字体继承, Style, ControlTemplate, SystemResource, VisualState ...

  8. 重新想象 Windows 8 Store Apps (16) - 控件基础: 依赖属性, 附加属性, 控件的继承关系, 路由事件和命中测试

    原文:重新想象 Windows 8 Store Apps (16) - 控件基础: 依赖属性, 附加属性, 控件的继承关系, 路由事件和命中测试 [源码下载] 重新想象 Windows 8 Store ...

  9. 重新想象 Windows 8 Store Apps (13) - 控件之 SemanticZoom

    原文:重新想象 Windows 8 Store Apps (13) - 控件之 SemanticZoom [源码下载] 重新想象 Windows 8 Store Apps (13) - 控件之 Sem ...

随机推荐

  1. 一篇文章,读懂 Netty 的高性能架构之道

    原文 Netty是一个高性能.异步事件驱动的NIO框架,它提供了对TCP.UDP和文件传输的支持,作为一个异步NIO框架,Netty的所有IO操作都是异步非阻塞的,通过Future-Listener机 ...

  2. 我常用的Mac快捷键

    1. 最小化当前窗口 command m 2. 在不同应用间切换 command tab 3. 在同一应用的不同窗口间切换 command ` 4. 在浏览器同一窗口的不同标签间切换 ctrl tab ...

  3. JVM -Xss调整Stack Space的大小 【转】

    Java程序中,每个线程都有自己的Stack Space.这个Stack Space不是来自Heap的分配.所以Stack Space的大小不会受到-Xmx和-Xms的影响,这2个JVM参数仅仅是影响 ...

  4. Django 新人开发的十个注意点

    总结一下 Django开发中,注意的事项,特别是新人,由于水平有限,也只能到这个层次,更多模式思想性的东西,还得在开发中慢慢体会. 1.各个APP独立,做到项目的模块分明.说的有点大,列几个列子优先 ...

  5. 在GridView中使用radioButoon

    在GridView中使用radioButoon 方法一: <input type="radio" id='radioSelectFeed' name="radioD ...

  6. 使用Object的wait,notify,notifyAll做线程调度

    我们知道java中的所有类的祖先都是Object,Object类有四个个方法wait(),wait(long timeout),notify(),notifyAll(),这四个方法可以用来做线程的调度 ...

  7. 国行手机安装GOOGLE PLAY

    原文地址:http://blog.sina.com.cn/s/blog_68cff87b0101a96k.html 相信国行的手机都是没有google Play 功能的吧,相比其它国外的手机,功能上逊 ...

  8. CLR via C#深解笔记五 - 事件

    事件处理实际上是一种具有特殊签名的delegate, 像这个样子:public delegate void EventHandler(object sender, EventArgs e);   类型 ...

  9. mysql ODBC connector相关问题

    mysql ODBC connector我安装了,怎么就不成功了 进到命令行,运行下边的:C:\>cd \windows\SysWOW64 C:\Windows\SysWOW64>odbc ...

  10. 谷歌正式发布Google APIs Client Library for .NET

    好消息,特大好消息! 英文原文:Google API library for .NET paves the way for Google services on Windows phone 本月 17 ...