2012-04-14 90 views
1

我正在嘗試使用for循環來限制添加到列表框的項目數。我使用Web客戶端拉下JSON數據。數據總是有24項,我想將其限制爲4.這裏是循環:WP7循環限制添加到列表框的項目數

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項。謝謝您的幫助。

UPDATE

這裏是我的班

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> 

回答

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); 
} 
+0

謝謝你的工作。如果你不介意,我還有一個額外的問題。我試圖拉'artistName',但它不工作。查看我的班級更新 – nos9 2012-04-15 00:09:27

+1

@ nos9 - 當您說您正在嘗試'拉入'artistName'時,您的問題並不清楚,但它不起作用。什麼不工作,你想做什麼? – 2012-04-15 00:14:41

+0

在我的應用程序中,我想顯示發佈名稱,發佈藝術家和圖像。使用您提供的名稱和圖像的代碼顯示4個項目,但藝術家的名字沒有。我已經在上面添加了我的xaml。 – nos9 2012-04-15 00:27:21