private async Task DoLoadNextChunkAsync(object param) { if (InProgress) { return; } InProgress = true; var dispatcher = Windows.UI.Xaml.Window.Current.Dispatcher; ThreadPool.RunAsync(async(a) => { var posts = await _repositoryService.GetPostsAsync(_displayedPosts, DOWNLOAD_CHUNK_SIZE, true); dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => { if (Posts.Last() is LoadNextListItemViewModel) { var last = Posts.Last(x => x is LoadNextListItemViewModel); if (last != null) { Posts.Remove(last); } } if (posts != null) { foreach (var post in posts) { PostItemViewModel vm = null; vm = PostItemVMFactory.CreateViewModel(post); if (vm != null) { Posts.Add(vm); } } _displayedPosts += posts.Count; if (_displayedPosts < _repositoryService.TotalNumberOfPosts) { Posts.Add(new LoadNextListItemViewModel(_resourceLoader.GetString("DownloadingPosts"), this)); } } InProgress = false; UpdateBindedProperties(); }); }); }
private async Task LoadPosts(int start, int number) { var posts = await _repositoryService.GetPostsAsync(start, number, true); if (posts != null) { foreach (var post in posts) { PostItemViewModel vm = null; vm = PostItemVMFactory.CreateViewModel(post); if (vm != null) { Posts.Add(vm); } } _displayedPosts = posts.Count; if (_displayedPosts < _repositoryService.TotalNumberOfPosts) { Posts.Add(new LoadNextListItemViewModel(_resourceLoader.GetString("DownloadingPosts"), this)); } UpdateBindedProperties(); } }