for ループを使用して、リストボックスに追加されるアイテムの数を制限しようとしています。Web クライアントを使用して 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>