更好的方法是使用Reactive Extension
。
首先看看我在這post的答案。它會告訴你你需要什麼dll以及一些有用的鏈接。
基本上,您需要將圖像存儲在集合中,然後使用Rx
(GenerateWithTime
)基於集合創建具有時間維度的可觀察序列。最後你調用一個方法來添加一個圖像並將其訂閱到可觀察序列。
這裏是一個工作例子,
private void MainPage_Loaded(object sender, RoutedEventArgs e)
{
// create a collection to store your 5 images
var images = new List<Image>
{
new Image() { Source = new BitmapImage(new Uri("/ApplicationIcon.png", UriKind.Relative)), Width = 120, Height = 120 },
new Image() { Source = new BitmapImage(new Uri("/ApplicationIcon.png", UriKind.Relative)), Width = 120, Height = 120 },
new Image() { Source = new BitmapImage(new Uri("/ApplicationIcon.png", UriKind.Relative)), Width = 120, Height = 120 },
new Image() { Source = new BitmapImage(new Uri("/ApplicationIcon.png", UriKind.Relative)), Width = 120, Height = 120 },
new Image() { Source = new BitmapImage(new Uri("/ApplicationIcon.png", UriKind.Relative)), Width = 120, Height = 120 }
};
// create a time dimension (2 seconds) to the generated sequence
IObservable<Image> getImages = Observable.GenerateWithTime(0, i => i <= images.Count - 1, i => images[i], _ => TimeSpan.FromSeconds(2), i => ++i);
// subscribe the DisplayOneImage handler to the sequence
getImages.ObserveOnDispatcher().Subscribe(DisplayOneImage);
}
private void DisplayOneImage(Image image)
{
// MyItems is an ItemsControl on the UI
this.MyItems.Items.Add(image);
}
希望這有助於。 :)