Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.Diagnostics;
- using System.IO;
- using System.Linq;
- using System.Runtime.InteropServices.WindowsRuntime;
- using System.Threading;
- using System.Threading.Tasks;
- using Windows.Foundation;
- using Windows.Foundation.Collections;
- using Windows.UI.Core;
- using Windows.UI.Xaml;
- using Windows.UI.Xaml.Controls;
- using Windows.UI.Xaml.Controls.Primitives;
- using Windows.UI.Xaml.Data;
- using Windows.UI.Xaml.Input;
- using Windows.UI.Xaml.Media;
- using Windows.UI.Xaml.Navigation;
- // The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409
- namespace TestIncremental
- {
- /// <summary>
- /// An empty page that can be used on its own or navigated to within a Frame.
- /// </summary>
- public sealed partial class MainPage : Page
- {
- public MainPage()
- {
- this.InitializeComponent();
- List.ItemsSource = new TestCollection();
- }
- }
- public abstract class SemaphoreCollection<T> : ObservableCollection<T>, ISupportIncrementalLoading
- {
- private CoreDispatcher _dispatcher;
- private SemaphoreSlim _wait;
- public bool HasMoreItems { get; set; }
- public SemaphoreCollection()
- {
- _dispatcher = Window.Current.Dispatcher;
- _wait = new SemaphoreSlim(0);
- HasMoreItems = true;
- }
- public IAsyncOperation<LoadMoreItemsResult> LoadMoreItemsAsync(uint count)
- {
- return Task.Run(() =>
- {
- LoadMoreItemsInternal(count);
- _wait.Wait();
- return new LoadMoreItemsResult { Count = count };
- }).AsAsyncOperation();
- }
- private async void LoadMoreItemsInternal(uint count)
- {
- await _dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () =>
- {
- await LoadMoreItems(count);
- _wait.Release();
- });
- }
- protected abstract Task LoadMoreItems(uint count);
- }
- public class TestCollection : SemaphoreCollection<string>
- {
- protected override async Task LoadMoreItems(uint count)
- {
- await Task.Delay(1000);
- for (int i = 0; i < count; i++)
- {
- Add($"Item {Count}");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement