将GenericList传递给Observable Collection

本文关键字:Observable Collection GenericList | 更新日期: 2023-09-27 18:11:50

我有Generic List

GetAllSponsor sponsorlist = new GetAllSponsor();
sponsorlist.getallsponsor();
string spnsrlst = sponsorlist.ToFormattedJsonString();
Sponsors sponsorObjectData = JsonConvert.DeserializeObject<Sponsors>(spnsrlst);

赞助商类定义如下:

public class Sponsors
        {
            public string id { get; set; }
            public string company_name { get; set; }
            public string description { get; set; }
            public string sponsor_level { get; set; }
            public string picture { get; set; }
            public string location { get; set; }
            public string website_url { get; set; }
            public string sm_twitter { get; set; }
            public string sm_facebook { get; set; }
            public string sm_linkedin { get; set; }
            public string sm_pinterest { get; set; }
            public string contact_number { get; set; }
            public string attachments { get; set; }
            public string date_time { get; set; }

        }

我需要把它传递给Observable Collection

我该怎么做?

将GenericList传递给Observable Collection

List<SomeType> list = new List<SomeType>();
// add items to list 
ObservableCollection<SomeType> collection = new ObservableCollection<SomeType>(list);

总结一下我们的聊天,我将把下载代码移出该类。该类应该被称为赞助商(因为它代表单个赞助商),并且它应该只是为您正在接收的数据字段(ID, company_name, description等)保存公共属性。您正在获得的JSON实际上是赞助商的数组,因此您需要将其反序列化为List<Sponsor>或适当的东西。然后,循环遍历列表,并将每个赞助商添加到ObservableCollection中,如下所示:

foreach (Sponsor s in mySponsors) 
{
   myCollection.Add(s);
}