2011-01-13 47 views
1

在我的(Silverlight)天气应用程序中,我从一个网站下载了多达6个独立的天气雷达图像(每个图像需要约20分钟),我需要做的是每秒显示一幅图像,然后在循环,暂停2秒,然后再次启动循环。 (这意味着图像循环将播放,直到用户点击后面或主页按钮,这是我想要的。)如何在WP7应用程序的循环中显示多个图像?

所以,我有一个RadarImage类如下,每个图像正在下载(通过WebClient)和然后装入RadarImage的实例,然后添加到集合(即:List<RadarImage>)...

//Following code is in my radar.xaml.cs to download the images.... 
int imagesToDownload = 6; 
int imagesDownloaded = 0; 
RadarImage rdr = new RadarImage(<image url>); //this happens in a loop of image URLs 
rdr.FileCompleteEvent += ImageDownloadedEventHandler; 

//This code in a class library. 
public class RadarImage 
{ 
    public int ImageIndex; 
    public string ImageURL; 
    public DateTime ImageTime; 
    public Boolean Downloaded; 
    public BitmapImage Bitmap; 

    private WebClient client; 

    public delegate void FileCompleteHandler(object sender); 
    public event FileCompleteHandler FileCompleteEvent; 

    public RadarImage(int index, string imageURL) 
    { 
     this.ImageIndex = index; 
     this.ImageURL = imageURL; 

     //...other code here to load in datetime properties etc... 

     client = new WebClient(); 
     client.OpenReadCompleted += new OpenReadCompletedEventHandler(wc_OpenReadCompleted); 
     client.OpenReadAsync(new Uri(this.ImageURL, UriKind.Absolute)); 
    } 

    private void wc_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e) 
    { 
     if (e.Error == null) 
     { 
      StreamResourceInfo sri = new StreamResourceInfo(e.Result as Stream, null); 
      this.Bitmap = new BitmapImage(); 
      this.Bitmap.SetSource(sri.Stream); 
      this.Downloaded = true; 
      FileCompleteEvent(this);   //Fire the event to let the app page know to add it to it's List<RadarImage> collection 
     } 
    } 
} 

正如你所看到的,在上面的类我所暴露的事件处理程序,让我的应用程序页面诀窍当每个图像已经下载时。当他们全部下载完成后,我会在我的xaml页面中运行以下代码 - 但只有最后一个图像出现,我无法解决原因!

private void ImageDownloadedEventHandler(object sender) 
    { 
     imagesDownloaded++; 
     if (imagesDownloaded == imagesToDownload) 
     { 
      AllImagesDownloaded = true; 

      DisplayRadarImages(); 
     } 
    } 

    private void DisplayRadarImages() 
    { 
     TimerSingleton.Timer.Stop(); 

     foreach (RadarImage img in radarImages) 
     { 
      imgRadar.Source = img.Bitmap; 
      Thread.Sleep(1000); 
     } 

     TimerSingleton.Timer.Start(); //Tick poroperty is set to 2000 milliseconds 
    } 

    private void SingleTimer_Tick(object sender, EventArgs e) 
    { 
     DisplayRadarImages(); 
    } 

所以你可以看到,我有停止(如果正在运行),则循环显示第二每个图像的定时器类的静态实例。当所有6个显示都会暂停,定时器启动,两秒后再次调用DisplayRadarImages()。

但正如我以前说过,我永远只能得到最后的形象示出于某种原因,我似乎无法得到这个工作正常。

我对WP7开发相当陌生(尽管不是.Net),所以只是想知道如何做到这一点 - 我想用Web浏览器控件试试这个,但肯定必须有一个更优雅的方式来循环通过一堆图像!

对不起,这是如此之久,但任何帮助或建议将非常感激。

迈克

+1

FYI WP7使用Silverlight(基于版本3),它是WPF子集的超集。这不是WPF的 – 2011-01-13 11:27:56

+0

是的,我的错误 - 但谢谢你指出。 – nzmike 2011-01-15 00:25:01

回答

4

您可以使用一个后台线程与定时器或睡眠定期更新你的图像控制。

Phạm Tiểu Giao - Threads in WP7

你需要派遣更新到UI与

Dispatcher.BeginInvoke(() => { /* your UI code */ }); 
+0

谢谢米克 - 我会试试看看它是怎么回事。你没有任何可以证明你这样做的代码? (这篇文章是有用的,但似乎没有具体说明这一点....显然谷歌它)。 – nzmike 2011-01-15 00:24:31

1

你为什么不增加一倍的最后一个图像到radarImages,定时器设置为1000,并显示一个图片在每个刻度上?

相关问题