コード例 #1
0
ファイル: NewsUKViewModel.cs プロジェクト: syedhoque89/RSS
        void ExecuteLoadItemsCommand()
        {
            IsBusy = true;

            newsServiceSubscription = NewsService.Get("https://feeds.bbci.co.uk/news/uk/rss.xml")
                                      .ObserveOn(SynchronizationContext.Current)
                                      .Subscribe(OnNext, OnError, OnComplete);

            void OnNext(IEnumerable <NewsItem> items)
            {
                var newsItems = items.ToList();

                if (!newsItems.Any())
                {
                    return;
                }

                //newsItems = items.OrderByDescending(i => i.PubDate).ToList();
                var news = new List <News>();

                foreach (var newsItem in newsItems)
                {
                    news.Add(new News
                    {
                        Title     = newsItem.Title.Text,
                        Summary   = newsItem.Description.Text,
                        Thumbnail = newsItem.MediaThumbnail.Url,
                        Url       = newsItem.Link.AbsoluteUri
                    });
                }

                NewsItems.ReplaceRange(news);
            }

            void OnError(Exception ex)
            {
                IsBusy        = false;
                ErrorOccurred = true;
                Debug.WriteLine(ex);
            }

            void OnComplete()
            {
                IsBusy = false;

                ErrorOccurred &= !NewsItems.Any();
            }
        }