2013-05-15 44 views
0

我有一个Flickr上的图像的URI列表。我在Windows phone Toolkit中使用手势来显示图像并处理轻弹事件。由于图片在网络上,源代码设置正常,但进度条在轻拂时立即折叠(隐藏),因为它已经设置了图像和手机的来源,仍然需要下载并显示它。如何在Windows Phone 8中显示来自网络的图片的进度条?

我希望显示进度条,直到图像完全可见。 (使用Web客户端有用吗?)

下面是一个简单的视频显示到底发生了什么。不介意这些照片,我只是拿起了第一个出来的东西。

Video Link

的代码如下:

private void GestureListener_Flick(object sender, FlickGestureEventArgs e) 
       { 
        if (((e.Angle <= 180 && e.Angle >= 135) || (e.Angle < 225 && e.Angle > 180)) && e.Direction.ToString().Equals("Horizontal")) 
        { 
         progess_bar.Visibility=Visibility.Visible; 
         if(index<photoslist.Count-1) 
         index++; 
         image.Source = new BitmapImage(new Uri(photoslist[index].LargeUrl , UriKind.Absolute)); 
         progess_bar.Visibility = Visibility.Collapsed; 
        } 
        else if (((e.Angle <= 45 && e.Angle >= 0) || (e.Angle < 360 && e.Angle >= 315)) && e.Direction.ToString().Equals("Horizontal")) 
        { 
         progess_bar.Visibility = Visibility.Visible; 
         if (index > 0) 
          index--; 
         else 
          index = 0; 
         image.Source = new BitmapImage(new Uri(photoslist[index].LargeUrl, UriKind.Absolute)); 
         progess_bar.Visibility = Visibility.Collapsed; 
        } 
       } 

回答

0

设置在图像源属性确实块正在运行的线程,直至下载完成这样的代码将继续执行,而你的进度条会立即被无形。

如果要提供真正的进度条功能,那么你就必须手动下载图像和更新基于进度的进度条。但是,图像通常不是大文件。您最好展示某种加载动画,直到图像下载完成。

+0

所以我基本上会等待一个webclient的downloadasync函数,并根据它显示下载进度。我确实想到了这一点,但会比我想要的更多地进行编码。 必须有一个更简单的方法 –

+0

如果你想捕获下载进度,你必须编写代码,否则没有其他方式获得通知。 –

+0

是的,我做了更多的研究,等待异步电话是取得进展的方式 感谢您的回复 –

0

对于那些有兴趣我有粗糙的代码解决方案,低于该作品。 (请注意,它没有优化,你可以在它上面工作)

private void GestureListener_Flick(object sender, FlickGestureEventArgs e) 
     { 
      if (((e.Angle <= 180 && e.Angle >= 135) || (e.Angle < 225 && e.Angle > 180)) && e.Direction.ToString().Equals("Horizontal")) 
      { 
       if (index < photoslist.Count - 1) 
       { 
        index++; 
        downloadImage(); 
       } 
       //image_big.Source = new BitmapImage(new Uri(photoslist[index].LargeUrl , UriKind.Absolute)); 
      } 
      else if (((e.Angle <= 45 && e.Angle >= 0) || (e.Angle < 360 && e.Angle >= 315)) && e.Direction.ToString().Equals("Horizontal")) 
      { 
       if (index > 0) 
       { 
        index--; 
        downloadImage(); 
       } 
       //image_big.Source = new BitmapImage(new Uri(photoslist[index].LargeUrl, UriKind.Absolute)); 

      } 
     } 

     private void downloadImage() 
     { 
      progess_bar.Visibility = Visibility.Visible; 
      WebClient wc = new WebClient(); 
      wc.OpenReadCompleted += new OpenReadCompletedEventHandler(wc_OpenReadCompleted); 
      wc.OpenReadAsync(new Uri(photoslist[index].LargeUrl, UriKind.Absolute), wc); 
     } 

     private void wc_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e) 
     { 
      if (e.Error == null && !e.Cancelled) 
      { 
       try 
       { 
        BitmapImage image = new BitmapImage(); 
        image.SetSource(e.Result); 
        image_big.Source = image; 
        progess_bar.Visibility = Visibility.Collapsed; 
       } 
       catch (Exception ex) 
       { 
        MessageBox.Show("Cannot get the feed right now.Please try later."); 
       } 
      } 
     } 
    } 
相关问题