Windows Phone 7下载多个图像

时间:2012-09-14 05:06:21

标签: image windows-phone-7 webclient

我正在制作类似于Windows Phone 7.5的应用程序的flexster。我有一段代码,我希望得到即将上映的电影和电影。但是当我的代码完成时,只有第一张图片会在列表框图像中填充。这是我的代码:

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;
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

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