我正在尝试使用for循环来限制添加到列表框中的项目数。我正在使用webclient下拉JSON数据。数据总是有24个项目,我想将它限制为4.这是循环:
public void jsonHome_GetDataCompleted(object snder, DownloadStringCompletedEventArgs e)
{
NewReleasesCharts homeData = JsonConvert.DeserializeObject<NewReleasesCharts>(e.Result);
int limit = 4;
for (int i = 0; i <= limit; i++)
{
FeaturedReleases release = homeData.results.featuredReleases[i];
int releaseID = release.id;
string releaseName = release.name;
string releaseImg = release.images.large.url;
new ReleaseLarge()
{
url = releaseImg
};
new FeaturedReleases()
{
id = releaseID,
name = releaseName
};
}
this.listRelease.ItemsSource = homeData.results.featuredReleases;
}
这可行但仍显示所有24项。谢谢你的帮助。
更新
这是我的课程
public class NewReleasesCharts
{
//public Metadata metadata { get; set; }
public ResultHome results = new ResultHome();
public IEnumerator<ResultHome> GetEnumerator()
{
return this.results.GetEnumerator();
}
}
public class ResultHome
{
public List<FeaturedReleases> featuredReleases { get; set; }
//public List<FeaturedCharts> featuredCharts { get; set; }
//public List<TopDownloads> topdownloads { get; set; }
//public List<MostPopularReleases> mostPopularReleases { get; set; }
//public List<Components> components { get; set; }
internal IEnumerator<ResultHome> GetEnumerator()
{
throw new NotImplementedException();
}
}
public class FeaturedReleases
{
public int id { get; set; }
public string type { get; set; }
public string name { get; set; }
public string slug { get; set; }
public ReleaseImage images { get; set; }
public List<Artists> artists { get; set; }
}
public class Artists
{
public int artistid { get; set; }
public string artistName { get; set; }
}
public class ReleaseImage
{
//public ReleaseSmall small { get; set; }
public ReleaseMedium medium { get; set; }
public ReleaseLarge large { get; set; }
}
public class ReleaseMedium
{
public int width { get; set; }
public int height { get; set; }
public string url { get; set; }
public string secureUrl { get; set; }
}
public class ReleaseLarge
{
public int width { get; set; }
public int height { get; set; }
public string url { get; set; }
public string secureUrl { get; set; }
}
和xaml
<ListBox Grid.Row="0" x:Name="listRelease">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<toolkit:WrapPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical" >
<toolkit:HubTile Source="{Binding images.large.url}" Margin="10" />
<TextBlock Text="{Binding name}" Width="173" />
<TextBlock Text="{Binding artists.artistName}" Width="173" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
答案 0 :(得分:2)
该代码正在创建新对象,但不会将它们添加到列表中。我不确定您要使用哪个对象(ReleaseLarge或FeaturedRelease)。尝试在存储在homeData中的反序列化JSON结果上使用linq。
public void jsonHome_GetDataCompleted(object snder, DownloadStringCompletedEventArgs e)
{
NewReleasesCharts homeData = JsonConvert.DeserializeObject<NewReleasesCharts>(e.Result);
const int limit = 4;
this.listRelease.ItemsSource = homeData.results.featuredReleases.Take(limit);
}