将GenericList传递给Observable Collection

时间:2016-10-06 21:11:20

标签: c# xamarin

我有通用列表

GetAllSponsor sponsorlist = new GetAllSponsor();
sponsorlist.getallsponsor();
string spnsrlst = sponsorlist.ToFormattedJsonString();

Sponsors sponsorObjectData = JsonConvert.DeserializeObject<Sponsors>(spnsrlst);

这是Sponsors Class定义的样子:

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

我怎么能这样做?

2 个答案:

答案 0 :(得分:1)

List<SomeType> list = new List<SomeType>();

// add items to list 

ObservableCollection<SomeType> collection = new ObservableCollection<SomeType>(list);

答案 1 :(得分:0)

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

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