Windows 8 Store Apps學習(54) 綁定: 增量方式載入資料

來源:互聯網
上載者:User

介紹

重新想象 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 = 0;            /// <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(1000);                                   // 增量載入的起始索引                               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));        }    }}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.