增量加载集合未使用usng社区工具包显示数据

时间:2018-07-23 15:21:27

标签: uwp windows-community-toolkit

我正在使用uwp社区工具包的增量加载集合。我更改了PeopleSource类,代码如下。

namespace Test
{
    public class TuchongSource : IIncrementalSource<TuchongImageMine>
    {
        private ObservableCollection<TuchongImageMine> _mine;
        public TuchongSource()
        {
            (Application.Current as App).bRefresh = true;
            _mine = new ObservableCollection<TuchongImageMine>();
            GetFirstData();
        }


        public async Task<IEnumerable<TuchongImageMine>> GetPagedItemsAsync(int pageIndex, int pageSize, CancellationToken cancellationToken = default(CancellationToken))
        {
            IEnumerable<TuchongImageMine> result = null;

            (Application.Current as App).currentPage++;
            ObservableCollection<TuchongImageMine> MineItems = new ObservableCollection<TuchongImageMine>();

            string url = "";
            if ((Application.Current as App).bRefresh)
                url = "https://api.tuchong.com/feed-app";
            else
                url = "https://api.tuchong.com/feed-app?post_id=" + (Application.Current as App).lastPostId + "&page=" + (Application.Current as App).currentPage.ToString() + "&type=loadmore";

            MineItems = await TuchongHelper.GetTuchongImage(url);
            foreach (var item in MineItems)
            {
                if ((Application.Current as App).toShowItems.Contains(item))
                    continue;
                (Application.Current as App).toShowItems.Add(item);
                _mine.Add(item);
                await Task.Delay(50);
            }
            (Application.Current as App).lastPostId = MineItems[MineItems.Count - 1].post_id;
            (Application.Current as App).bRefresh = false;

            result = (Application.Current as App).toShowItems.AsEnumerable<TuchongImageMine>();
            return result;
        }

        private async Task GetFirstData()
        {
            string url = "https://api.tuchong.com/feed-app";
            _mine = await TuchongHelper.GetTuchongImage(url);
            (Application.Current as App).lastPostId = _mine[_mine.Count - 1].post_id;
        }
    }
}

在MainPage.xaml.cs中,我使用

var ss = new IncrementalLoadingCollection<TuchongSource, TuchongImageMine>();
gridView.ItemsSource = ss;

但是我的GridView中什么都没有显示。我想知道为什么。

如果您想了解更多,这是我的项目https://github.com/hupo376787/IncrementalLoadingCollection.git

1 个答案:

答案 0 :(得分:0)

该问题是由异步方法创建您的ObservableCollection<TuchongImageMine> _mine对象引起的。在您从网站获取真实数据并将其分配给_mine类中的TuchongSource对象之前,已使用一个计数为0的IncrementalLoadingCollection<TuchongSource, TuchongImageMine>()对象创建了ObservableCollection<TuchongImageMine> _mine对象在MainPage.xaml.cs中。因此,您的GridView不显示任何内容。您可以将最合适的数据放在MainPage.xaml.cs中,但不能放在TuchongSource类的构造函数中。

这是我在MainPage.xaml.cs中更改的代码;

public MainPage()
{
    this.InitializeComponent();
    ApplicationView.GetForCurrentView().SetPreferredMinSize(new Size(188, 188));

    //GetRecommend();
    //gridView.ItemsSource = (Application.Current as App).toShowItems;
}

protected async override void OnNavigatedTo(NavigationEventArgs e)
{
    base.OnNavigatedTo(e);

    var ss1 = new IncrementalLoadingCollection<TuchongSource, TuchongImageMine>();
    var res = await HttpHelper.GetJsonAsync<TuchongImageStandard>("https://api.tuchong.com/feed-app");

    foreach (var item in res.feedList)
            {
                TuchongImageMine mine = new TuchongImageMine();
                try
                {
                    mine.post_id = item.post_id;
                    mine.url = item.url;
                    mine.author_id = item.author_id;
                    mine.published_at = item.published_at;
                    mine.passed_time = item.passed_time;
                    mine.favorites = item.favorites;
                    mine.comments = item.comments;
                    mine.views = item.views;
                    mine.title = item.title;
                    mine.image_count = item.image_count;

                    if (item.image_count <= 0)
                        continue;
                    List<TuchongImageMine.Images> mineImages = new List<TuchongImageMine.Images>();
                    foreach (var item0 in item.images)
                    {
                        TuchongImageMine.Images ii = new TuchongImageMine.Images();
                        ii.img_realurl = "https://photo.tuchong.com/" + item0.user_id.ToString() + "/f/" + item0.img_id + ".jpg";
                        ii.img_id = item0.img_id;
                        ii.user_id = item0.user_id;
                        ii.title = item0.title;
                        ii.excerpt = item0.excerpt;
                        ii.width = item0.width;
                        ii.height = item0.height;
                        ii.description = item0.description;

                        mineImages.Add(ii);
                    }
                    mine.coverimg_realurl = mineImages[0].img_realurl;
                    mine.coverimg = await HttpHelper.GetWebImageAsync(mineImages[0].img_realurl);

                    mine.post_id = item.post_id;
                    mine.tags = item.tags;
                    mine.event_tags = item.event_tags;
                    mine.url = item.url;
                    mine.data_type = item.data_type;
                    mine.created_at = item.created_at;
                    mine.sites = item.sites;
                    TuchongImageMine.Site ss = new TuchongImageMine.Site
                    {
                        site_id = item.site.site_id,
                        type = item.site.type,
                        name = item.site.name,
                        domain = item.site.domain,
                        description = item.site.description,
                        followers = item.site.followers,
                        url = item.site.url,
                        icon = item.site.icon,
                        verified = item.site.verified,
                        verified_type = item.site.verified_type,
                        verified_reason = item.site.verified_reason,
                        verifications = item.site.verifications,
                        is_following = item.site.is_following,
                    };
                    mine.site = ss;
                    mine.recom_type = item.recom_type;
                    mine.rqt_id = item.rqt_id;
                    mine.is_favorite = item.is_favorite;

                    ss1.Add(mine);
                }
                catch (Exception ex)
                {

                }
            }

    //ss.Add()
    gridView.ItemsSource = ss1;
    //DataContext = ss1;
}