예제 #1
0
        /// <summary>
        /// Databinded Command to add comic to list of comics
        /// </summary>
        public void AddComicExecute()
        {
            if (SelectedComic == null)
            {
                throw new PresentationException("Gelieve een strip te selecteren.");
            }
            ViewComic comic  = SelectedComic;
            int       amount = OrderAmount;

            ComicList.Add(new KeyValuePair <ViewComic, int>(comic, amount));
        }
        private async Task CurrentItemChanged(CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            var comicNumberToLoad = CurrentComic.Num - _cacheSize;

            if (comicNumberToLoad <= 0 || ComicList.Any(x => x.Num == comicNumberToLoad))
            {
                return;
            }

            var comic = await DownloadComicAsync(comicNumberToLoad);

            await Device.InvokeOnMainThreadAsync(() =>
            {
                ComicList.Add(comic);
            });
        }
        private async Task InitAsync()
        {
            try
            {
                // I don't know why this statement is slow (2 or 3 sec)...
                // So I moved it in my async init task.
                CurrentItemChangedCommand = ReactiveCommand.CreateFromTask(CurrentItemChanged);
                var firstModel = await DownloadComicAsync();

                var tasksList = new List <Task <XkcdComic> >();

                for (var i = 0; i < _cacheSize; ++i)
                {
                    tasksList.Add(DownloadComicAsync(firstModel.Num - 1 - i));
                }

                await Task.WhenAll(tasksList);

                await Device.InvokeOnMainThreadAsync(async() =>
                {
                    ComicList.Add(firstModel);
                    foreach (var task in tasksList)
                    {
                        ComicList.Add(await task);
                    }
                });
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
            finally
            {
                IsBusy = false;
            }
        }