实现WP7下ListBox分页加载接口
APP开发,列表的分页加载是再常见不过的需求,我以前对每个需要分页加载的列表控件都写了一套分页加载逻辑,后来看官方的Demo,才发现其实这些相似的代码是可以封装起来的。基于WinRT开发APP的人应该知道,在WinRT下有ISupportIncrementalLoading这么一个接口,这个接口一个很重要的作用就是可以实现列表控件的分页加载。比如ListView,当ListView的ItemSource绑定的数据源实现了ISupportIncrementalLoading接口,那么当滑动到ListView底部的时候,会自动调用ISupportIncrementalLoading的方法实现自动加载。
可是在基于Silverlight的WP上是没有ISupportIncrementalLoading这个接口的,那下面就在Wp7上来实现一个简易版的列表分页加载接口。
首先我们定义这个接口,这个接口只有一个方法:LoadMoreAsync,这个方法的作用就是根据参数加载数据,你可以把参数理解成PageSize。
public interface ISupportIncrementalLoading{Task<LoadMoreItemsResult> LoadMoreAsync(uint count);}
当我们加载一页数据,会有一些通用的逻辑,比如说判断是否还有下一页;比如说,在加载前后我们可能还要进行一些操作,这些都可以封装起来,所以我们定义一个抽象类来实现这个逻辑,此外,当我们加载数据往往是多条,并且要实现集合绑定,以便在界面显示新加载数据,这里为了简便我们直接让抽象类继承ObservableCollection<T>。抽象类定义如下:
public abstract class IncrementalLoadingDataSource<T> : ObservableCollection<T>, ISupportIncrementalLoading{public async Task<LoadMoreItemsResult> LoadMoreAsync(uint count){if (CanLoadMore()){ICollection<T> list = await LoadMore(count);foreach (var item in list){this.Add(item);}return new LoadMoreItemsResult() { LoadedCount = list.Count };}else{return new LoadMoreItemsResult() { LoadedCount = 0 };;}}protected abstract Task<ICollection<T>> LoadMore(uint count);protected abstract bool CanLoadMore();}
抽象类的实现逻辑比较简单,主要是判断是否还有下一页,如果有就请求下一页数据。这里有两个抽象方法我们没有实现,因为真正加载数据以及判断是否还有下一页可能都是跟业务相关,需要各自具体实现的,因此这里没有具体实现。这里给出一个实现样例,比如我们要实现StudentInfo的分页加载,可以如下定义:
public class StudentInfo{public string Name { get; set; }}public class Students : IncrementalLoadingDataSource<StudentInfo>{protected async override Task<ICollection<StudentInfo>> LoadMore(uint count){return await Task.Factory.StartNew<ICollection<StudentInfo>>(() => {List<StudentInfo> list = new List<StudentInfo>();for (int i = 0; i < count; i++){list.Add(new StudentInfo() { Name = i.ToString() });}return list;});}protected override bool CanLoadMore(){return true;}}
现在接口都已经定义好了,我们的目的是让所有继承自IncrementalLoadingDataSource的实体数据都可以分页加载。
我们该如何把接口ISupportIncrementalLoading与我们的列表控件关联起来?这里我们借助附加属性来实现,我们定义一个类叫IncrementalLoadingHelper,类中定义一个附加属性叫IncrementalLoading,只有当列表控件的IncrementalLoading为true时,并且列表控件的ItemsSource的数据源实现ISupportIncrementalLoading就可以分页加载。IncrementalLoadingHelper定义如下,其主要逻辑是在设置IncrementalLoading时,获取到列表控件,进而获取到列表控件的ScrollBar,并绑定ScrollBar的ValueChanged事件,在ValueChanged事件中,判断是否到底部,如果到底部,则调用列表控件的ItemsSource的LoadMoreAsync方法,前面介绍过,ItemsSource必须实现ISupportIncrementalLoading接口。
public class IncrementalLoadingHelper{public const uint PAGE_SIZE = 25;public static DependencyProperty IncrementalLoadingProperty =DependencyProperty.RegisterAttached("IncrementalLoading", typeof(Boolean), typeof(IncrementalLoadingHelper), new PropertyMetadata(false, IncrementalLoadingPropertyChangedCallback));private static ItemsControl _itemsControl;public static void SetIncrementalLoading(DependencyObject obj, bool val){obj.SetValue(IncrementalLoadingProperty, val);}public static bool GetIncrementalLoading(DependencyObject obj){return (bool)obj.GetValue(IncrementalLoadingProperty);}private static void IncrementalLoadingPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e){ItemsControl control = d as ItemsControl;ScrollBar scrollBar = GetChild<ScrollBar>(d);if (control != null && scrollBar != null && control.Items.Count > 0){scrollBar.Tag = control;_itemsControl = control;var isupport = control.ItemsSource as ISupportIncrementalLoading;if (isupport != null){scrollBar.ValueChanged += scrollBar_ValueChanged;}}}static async void scrollBar_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e){ScrollBar scrollBar = sender as ScrollBar;double max = (double)scrollBar.GetValue(ScrollBar.MaximumProperty);if (e.NewValue + 1 >= max){var control = scrollBar.Tag as ItemsControl;if (control != null){var result = await ((ISupportIncrementalLoading)control.ItemsSource).LoadMoreAsync(PAGE_SIZE);}}}public static T GetChild<T>(DependencyObject element) where T : DependencyObject{T result = null;Queue<DependencyObject> quene = new Queue<DependencyObject>();quene.Enqueue(element);while (quene.Count > 0){DependencyObject ele = quene.Dequeue();int count = VisualTreeHelper.GetChildrenCount(ele);for (int i = 0; i < count; i++){DependencyObject child = VisualTreeHelper.GetChild(ele, i);result = child as T;if (result != null) return result;quene.Enqueue(child);}}return result;}}
现在,假设我们页面上有一个需要实现学生分页加载的列表控件ListBox名字叫lstStudent,那么我们需要做以下两步就可以了:
1)在页面或者列表控件Loaded事件中设置附加属性IncrementaLoading的值为ture;
2)new一个新的Student实例赋值给lstStudent的ItemsSource。
private void LayoutRoot_Loaded(object sender, RoutedEventArgs e){lstStudent.SetValue(IncrementalLoadingHelper.IncrementalLoadingProperty, true);}public async void LoadData(){var data = new Students();await data.LoadMoreAsync(IncrementalLoadingHelper.PAGE_SIZE);lstStudent.ItemsSource = data;}
下拉一下,是不是可以分页加载了!
转载于:https://www.cnblogs.com/zhengldg/p/4257151.html
实现WP7下ListBox分页加载接口相关推荐
- 【Vue】ElementUI el-select 下拉分页加载数据,并支持搜索功能(此处不支持分页)
项目场景: 在公司管理后台需要有一个 根据选择客户筛选拥有的服务 的功能 问题描述: 但是有上千条用户数据,一次性查询加载会导致页面有很长时间(大概4~5s)的空白期,用户体验很不友好. 故需要进行优 ...
- jQuery实现简单实用的H5(手机端)下拉分页加载(所谓的懒加载!!!)
话不多说,直接码上 . <div> <div>展示内容列表块</div> //now当前页数 num总页数 这些需要在列表初始化的时候计算好,并赋值给此属性 ...
- 微信小程序实现下拉分页加载更多数据
1.使用scroll-view实现滚动下拉 wxml <scroll-view wx:if="{{plotArr.length > 0}}" scroll-y=&quo ...
- List 分页加载数据控制机制
分页加载是一种应用很广泛的数据展示控制机制,相信绝大多数开发者对于这一套机制都非常熟悉.这篇文章的主要目的结合实际的使用场景,对以往在开发中遇到一些概念进行梳理,归纳的同时加深理解,也希望能帮助更多刚 ...
- php下拉上滑分页,Flutter实现下拉刷新 上拉分页加载更多
一.Flutter实现下拉刷新和上拉分页加载更多 在Flutter官方sdk中给我们提供了下拉刷新的组件RefreshIndicator.但是没有提供上拉分页加载更多的组件.但是在Flutter Li ...
- VUE 列表页中实现分页加载(下拉到底部触发下一页 )
1.HTML结构 注:该方法所执行的基础是10条数据的高度总和应大于列表盒子固定高度 <ul class="my_list" @scroll="scrollEven ...
- html5 加载下一页,html5上划实现分页加载
一.功能分析 当页面有许多相同类型数据需要加载,而且单个屏幕不足以显示这些内容,就需要使用分页加载方法,在手机客户端,一般都是使用上划下划的方式,为了有更好的用户体验,还需要在加载数据前,显示类似&q ...
- 微信小程序云数据库触底分页加载,下拉无限加载,第一次请求数据随机,随机获取数据库的数据
效果图 小程序云开发分页加载代码 <!--pages/chatList/chatList.wxml--> <view class="pageTitle">家 ...
- 微信小程序自定义下拉选择框与分页加载--自用【随笔】
效果图展示 下拉加载 上拉加载 编码实现 wxml <!--pages/unitmanage/unitlease/unitlease.wxml--> <view><vie ...
- 【Vue学习总结】22.使用Mint UI的infinite-scroll实现上拉分页加载
接上篇<21.Vue-UI框架之Mint UI的使用> 上一篇我们讲解了Vue的UI框架Mint UI的使用,本篇我们来通过Mint UI提供的infinite-scroll组件结合api ...
最新文章
- LeetCode 之 JavaScript 解答第20题 —— 有效的括号(Valid Parentheses)
- Python:numpy库中的一些函数简介、使用方法之详细攻略
- 可以直接反编译Mips架构的IDA版本(无需安装脚本)
- 抓住大数据时代的红利
- java获取文件夹 路径,Java获取文件的路径
- 流程控制: jQ Deferred 与 ES6 Promise 使用新手向入坑!
- 用python的进行压缩文件
- java day24【Stream流、方法引用】
- ASP.NET留言板 文字加表情
- shell编程规范与变量
- regester正则用法_Regester(正则表达式测试器)
- 用神经网络分类根号2与根号x的数据汇总
- 正则表达式匹配中文及符号、英文及符号数字空格换行符及常用正则表达式
- ABAP——smartform添加图片
- Tkinter Canvas画布 图片导入
- 网易邮箱添加附件功能原理浅析
- php跳水比赛,2018年“迎春杯”海南省青少年健身游泳比赛成功举行 前奥运跳水冠军许艳梅见证海南游泳健儿风采...
- android 2.3.6Gallary源码导入到Eclipse中编译
- 简单弄懂Saas是什么? Saas与传统软件有什么区别?
- CVPR 2017论文集锦(论文分类)—— 附录部分翻译