Advertisement
FrayxRulez

ISupportIncrementalLoading on UI thread

Nov 9th, 2015
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.44 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Diagnostics;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Runtime.InteropServices.WindowsRuntime;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. using Windows.Foundation;
  11. using Windows.Foundation.Collections;
  12. using Windows.UI.Core;
  13. using Windows.UI.Xaml;
  14. using Windows.UI.Xaml.Controls;
  15. using Windows.UI.Xaml.Controls.Primitives;
  16. using Windows.UI.Xaml.Data;
  17. using Windows.UI.Xaml.Input;
  18. using Windows.UI.Xaml.Media;
  19. using Windows.UI.Xaml.Navigation;
  20.  
  21. // The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409
  22.  
  23. namespace TestIncremental
  24. {
  25.     /// <summary>
  26.     /// An empty page that can be used on its own or navigated to within a Frame.
  27.     /// </summary>
  28.     public sealed partial class MainPage : Page
  29.     {
  30.         public MainPage()
  31.         {
  32.             this.InitializeComponent();
  33.  
  34.             List.ItemsSource = new TestCollection();
  35.         }
  36.     }
  37.  
  38.     public abstract class SemaphoreCollection<T> : ObservableCollection<T>, ISupportIncrementalLoading
  39.     {
  40.         private CoreDispatcher _dispatcher;
  41.         private SemaphoreSlim _wait;
  42.  
  43.         public bool HasMoreItems { get; set; }
  44.  
  45.         public SemaphoreCollection()
  46.         {
  47.             _dispatcher = Window.Current.Dispatcher;
  48.             _wait = new SemaphoreSlim(0);
  49.  
  50.             HasMoreItems = true;
  51.         }
  52.  
  53.         public IAsyncOperation<LoadMoreItemsResult> LoadMoreItemsAsync(uint count)
  54.         {
  55.             return Task.Run(() =>
  56.             {
  57.                 LoadMoreItemsInternal(count);
  58.                 _wait.Wait();
  59.  
  60.                 return new LoadMoreItemsResult { Count = count };
  61.             }).AsAsyncOperation();
  62.         }
  63.  
  64.         private async void LoadMoreItemsInternal(uint count)
  65.         {
  66.             await _dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () =>
  67.             {
  68.                 await LoadMoreItems(count);
  69.                 _wait.Release();
  70.             });
  71.         }
  72.  
  73.         protected abstract Task LoadMoreItems(uint count);
  74.     }
  75.  
  76.     public class TestCollection : SemaphoreCollection<string>
  77.     {
  78.         protected override async Task LoadMoreItems(uint count)
  79.         {
  80.             await Task.Delay(1000);
  81.             for (int i = 0; i < count; i++)
  82.             {
  83.                 Add($"Item {Count}");
  84.             }
  85.         }
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement