0

我正在为 windows phone 7.5 制作类似 flexster 的应用程序。我有一段代码,我想获取即将上映的电影图片和电影 ID。但是当我的代码完成时,只有第一张图片会填充到列表框图像中。这是我的代码:

public void NowPlayinJson(string Uri)
{
  string apiUri = string.Format("{0}{1}{2}10", Uri, ApiKay, PagesLimit);
  WebClient Rclient = new WebClient();

  Rclient.DownloadStringAsync(new Uri(apiUri));
  Rclient.DownloadStringCompleted += (s, e) =>
  {
      if (e.Error == null)
      {
          jsonStringValue = (e.Result.ToString().Trim());
          ImageUri();
       }
  };
}
public void ImageUri()
{
    var ParseImageUri = JObject.Parse(jsonStringValue);
    var ParseToJson = 
        JsonConvert.DeserializeObject<RootObject(ParseImageUri.ToString());

    NowPlayingUri = ParseToJson.movies[0+a].posters.detailed;

    a++;
    DownloadImage();
}


public void DownloadImage()
{
    if (!web.IsBusy)
    {
        web.OpenReadAsync(new Uri(NowPlayingUri), UriKind.Absolute);
    }
    web.OpenReadCompleted += 
        new OpenReadCompletedEventHandler(web_OpenReadCompleted);
} 

void web_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
    if (e.Error == null)
    {
        while(i<6)
        {
            var stream = e.Result;

            var bi = new BitmapImage();
            bi.SetSource(stream);

            myNowPlayingList.Add(new NowPlaying() 
                { NowImage = bi, NowTitle = "title" + i });
            i++;
            if (b < 6)
            {
                b++;
                ImageUri();
            }
            if(b == 6)
            {
                b++;
                NowPlayingListBox.ItemsSource = myNowPlayingList;
            }
        }
    }
}
4

1 回答 1

0

我看到您没有维护任何queue待处理的图像下载或列表。请注意webClient can download one Image at a time, 所以如果它很忙,请将其添加到队列中。成功下载后检查队列长度。如果它不为空,则从队列中删除项目并将其添加到 webClient 下载。如果您maintain a cache或其他类似的东西,如果图像已经在缓存或 IS 中,不要从那里下载和显示,否则将图像添加到下载队列中。希望这种方法对您有所帮助。

于 2012-09-14T05:24:03.750 回答