重新想象 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) - 绑定: 增量方式加载数据的更多相关文章
- 重新想象 Windows 8 Store Apps (55) - 绑定: MVVM 模式
[源码下载] 重新想象 Windows 8 Store Apps (55) - 绑定: MVVM 模式 作者:webabcd 介绍重新想象 Windows 8 Store Apps 之 绑定 通过 M ...
- 重新想象 Windows 8 Store Apps (52) - 绑定: 与 Element Model Indexer Style RelativeSource 绑定, 以及绑定中的数据转换
[源码下载] 重新想象 Windows 8 Store Apps (52) - 绑定: 与 Element Model Indexer Style RelativeSource 绑定, 以及绑定中的数 ...
- 重新想象 Windows 8 Store Apps (53) - 绑定: 与 ObservableCollection CollectionViewSource VirtualizedFilesVector VirtualizedItemsVector 绑定
[源码下载] 重新想象 Windows 8 Store Apps (53) - 绑定: 与 ObservableCollection CollectionViewSource VirtualizedF ...
- 重新想象 Windows 8 Store Apps 系列文章索引
[源码下载][重新想象 Windows 8.1 Store Apps 系列文章] 重新想象 Windows 8 Store Apps 系列文章索引 作者:webabcd 1.重新想象 Windows ...
- 重新想象 Windows 8 Store Apps (23) - 文件系统: 文本的读写, 二进制的读写, 流的读写, 最近访问列表和未来访问列表
原文:重新想象 Windows 8 Store Apps (23) - 文件系统: 文本的读写, 二进制的读写, 流的读写, 最近访问列表和未来访问列表 [源码下载] 重新想象 Windows 8 S ...
- 重新想象 Windows 8 Store Apps (59) - 锁屏
[源码下载] 重新想象 Windows 8 Store Apps (59) - 锁屏 作者:webabcd 介绍重新想象 Windows 8 Store Apps 之 锁屏 登录锁屏,获取当前程序的锁 ...
- 重新想象 Windows 8 Store Apps (15) - 控件 UI: 字体继承, Style, ControlTemplate, SystemResource, VisualState, VisualStateManager
原文:重新想象 Windows 8 Store Apps (15) - 控件 UI: 字体继承, Style, ControlTemplate, SystemResource, VisualState ...
- 重新想象 Windows 8 Store Apps (16) - 控件基础: 依赖属性, 附加属性, 控件的继承关系, 路由事件和命中测试
原文:重新想象 Windows 8 Store Apps (16) - 控件基础: 依赖属性, 附加属性, 控件的继承关系, 路由事件和命中测试 [源码下载] 重新想象 Windows 8 Store ...
- 重新想象 Windows 8 Store Apps (13) - 控件之 SemanticZoom
原文:重新想象 Windows 8 Store Apps (13) - 控件之 SemanticZoom [源码下载] 重新想象 Windows 8 Store Apps (13) - 控件之 Sem ...
随机推荐
- Microsoft.Web.RedisSessionStateProvider 运行异常问题
System.TimeoutException: Timeout performing GET MyKey, inst: 2, mgr: Inactive, queue: 6, qu: 0, qs: ...
- SQL SERVER 服务启动后停止,某些服务由其它服务或程序使用时将自动停止
发生症状: 先是服务器挂掉,之后服务器可以登陆,但是实例却不能登陆进去(部分).出现的错误日志如下: :: R2 (SP2) - 10.50.4000.0 (X64) Jun :: Copyright ...
- EF: Returns multi table from procedure
原文:https://msdn.microsoft.com/en-us/data/jj691402.aspx
- 加锁解锁PHP实现 -转载
PHP并没有完善的线程支持,甚至部署到基于线程模型的httpd服务器都会产生一些问题,但即使是多进程模型下的PHP,也难免出现多进程共同访问同一资源的情况. 比如整个程序共享的数据缓存,或者因为资源受 ...
- 几种常用远程通信技术(RPC,Webservice,RMI,JMS)的区别
原文链接:http://blog.csdn.net/shan9liang/article/details/8995023 RPC(Remote Procedure Call Protocol) RPC ...
- iOS开发备忘录:自定义UINavigationBar背景图片和Back按钮
iOS项目,根据设计图,有时需要自定义UIView的UINavigationBar的背景.可以切出来一张1像素左右的背景图片,来充当UINavigationBar的背景. 可以利用Navigation ...
- OpenGL cubeMap
glsl 的reflect(I,N)其中I是 眼睛(camera)位置到顶点位置的方向向量,N为顶点法线,必须要归一化 橙宝书里给出的计算过程是这样的:reflect(I,N) = I - 2 *do ...
- CSS基础(五):定位
CSS定位机制 CSS 有三种基本的定位机制:相对定位.浮动和绝对定位. 相对定位 相对定位指的是设置为相对定位的元素框会偏移某个距离.元素仍然保持其未定位前的形状,它原本所占的空间仍保留. 如果将b ...
- C# WinForm程序打印条码 Code39码1
做WinForm程序需要打印条码,为了偷懒不想自己写生成条码的程序在网上下载一个标准的39码的字体,在程序里面换上这个条码字体即可打印条码了. 最重要的一点作为记录: 如果想把“123456789”转 ...
- 如何让js不产生冲突,避免全局变量的泛滥,合理运用命名空间
为了避免变量之间的覆盖与冲突,可以生成命名空间,命名空间是一种特殊的前缀,在js中,通过{ }对象实现. 在不同的匿名函数中,根据功能声明一个不同的命名空间,每个匿名函数中GLOBAL对象的属性都不直 ...