[英]How can I display multiple images in a loop in a WP7 app?
In my (Silverlight) weather app I am downloading up to 6 seperate weather radar images (each one taken about 20 mins apart) from a web site and what I need to do is display each image for a second then at the end of the loop, pause 2 seconds then start the loop again. 在我的(Silverlight)天气应用程序中,我从网站上下载了多达6张单独的天气雷达图像(每张图像相距约20分钟),我需要做的是将每个图像显示一秒钟,然后在循环结束时显示,请暂停2秒钟,然后再次开始循环。 (This means the loop of images will play until the user clicks the back or home button which is what I want.)
(这意味着将一直播放图像循环,直到用户单击我想要的后退或主页按钮为止。)
So, I have a RadarImage class as follows, and each image is getting downloaded (via WebClient) and then loaded into a instance of RadarImage which is then added to a collection (ie: List<RadarImage>
)... 因此,我有一个RadarImage类,如下所示,每个图像都通过WebClient下载(然后通过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
}
}
}
As you can see, in the class above I have exposed an event handler to let my app page know when each image has downloaded. 如您所见,在上面的类中,我公开了一个事件处理程序,以让我的应用页面知道何时下载了每个图像。 When they have all downloaded I then run the following code in my xaml page - but only the last image ever shows up and I can't work out why!
他们都下载完后,然后在我的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();
}
So you can see that I have a static instance of a timer class which is stopped (if running), then the loop should show each image for a second. 因此,您可以看到我有一个计时器类的静态实例,该实例已停止(如果正在运行),则循环应显示每个图像一秒钟。 When all 6 have been displayed then it pauses, the timer starts and after two seconds DisplayRadarImages() gets called again.
当所有6个都显示完后,它将暂停,计时器启动,两秒钟后再次调用DisplayRadarImages()。
But as I said before, I can only ever get the last image to show for some reason and I can't seem to get this working properly. 但是正如我之前所说,由于某种原因,我只能显示最后一张图像,而且似乎无法正常工作。
I'm fairly new to WP7 development (though not to .Net) so just wondering how best to do this - I was thinking of trying this with a web browser control but surely there must be a more elegant way to loop through a bunch of images! 我是WP7开发的新手(虽然不是.Net),所以只是想知道如何做到最好-我正在考虑通过Web浏览器控件尝试此操作,但是肯定有一种更优雅的方法可以遍历一堆图片!
Sorry this is so long but any help or suggestions would be really appreciated. 抱歉,这么长,但是任何帮助或建议都将不胜感激。
Mike 麦克风
You can use a background thread with either a Timer or Sleep to periodically update your image control. 您可以将后台线程与Timer或Sleep一起使用,以定期更新图像控件。
Phạm Tiểu Giao - Threads in WP7 PhạmTiểuGiao-WP7中的线程
You'll need to dispatch updates to the UI with 您需要使用以下命令向UI分发更新
Dispatcher.BeginInvoke( () => { /* your UI code */ } );
为什么不将最后一张图像添加两次到radarImages中,为什么将Timer设置为1000并在每个刻度上仅显示一张图像?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.