私の(Silverlight)天気アプリでは、ウェブサイトから別々の気象レーダー画像(それぞれ約20分間隔で撮影したもの)を6枚ダウンロードしています。私がする必要があるのは、各画像を1秒間表示してからループを2秒間停止し、ループを再開します。 (これは、ユーザーがバックボタンまたはホームボタンをクリックするまで再生されることを意味します)。WP7アプリケーションのループで複数の画像を表示するにはどうすればいいですか?
私はRadarImageクラスを次のように持っています。各画像はWebClient経由でダウンロードされています。あなたが見ることができるように私は私のアプリのページを知ることができるようにイベントハンドラを公開している上記のクラスでは、
//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
}
}
}
...:(List<RadarImage>
すなわち)、その後、コレクションに追加されRadarImageのインスタンスにロード各画像がダウンロードされたとき。彼らがすべてダウンロードしたら、私の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つすべてが表示されてから一時停止すると、タイマーが開始し、2秒後にもう一度DisplayRadarImages()が呼び出されます。
しかし、私が以前に言ったように、何らかの理由で最後の画像を表示することしかできず、正しく動作しないようです。
私はWP7開発(ネットではありません)にはかなり新しかったので、これをどうやって行うのが最善か疑問に思っています。これをウェブブラウザのコントロールで試してみたいと思っていましたが、画像の束を介して!
申し訳ありませんが、これは長すぎますが、助力や提案は本当に感謝しています。
マイク
FYI WP7では、WPFのサブセットのスーパーセットであるSilverlight(バージョン3ベース)を使用しています。これはWPFとしてsmaeではありません –
うん、私の間違い - しかし、それを指摘していただきありがとうございます。 – nzmike