私はc#とWPFが初めてで、フォルダーをループして1つずつ画像を表示するアプリケーションを構築しようとしています。最後のものが表示されたら、最初のものをもう一度表示する必要があります。
すべてのファイルに 1.jpg 、 2.jpg などの名前を付けてみました。その後、画像の量をループします。しかし、1つを削除すると、エラーが発生します。
これを達成するためのより良い方法はありますか?
私は C# と、グリッド内に画像がある WPF ウィンドウを使用しています。
どんな助けでも大歓迎です!
編集:現在のコード
private string[] files;
private System.Timers.Timer timer;
private int counter;
private int Imagecounter;
public IntroScreen()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(this.MainWindow_Loaded);
}
private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
setupPics();
}
private void setupPics()
{
timer = new System.Timers.Timer();
timer.Elapsed += new ElapsedEventHandler(timer_Tick);
timer.Interval = (2000);
timer.Enabled = true;
timer.Start();
files = Directory.GetFiles("../../Resources/Taken/", "*.jpg", SearchOption.TopDirectoryOnly);
Imagecounter = files.Length;
counter = 0;
}
private void timer_Tick(object sender, EventArgs e)
{
counter++;
Dispatcher.BeginInvoke(DispatcherPriority.Input, new ThreadStart(() =>
{
Picture.Source = new BitmapImage(new Uri(files[counter - 1], UriKind.Relative));
}));
if (counter == Imagecounter)
{
counter = 0;
}
}
これは機能していません。フォルダー内のアイテムが見つかりますが、画像は変わりません。
エラーは返されません。画像が表示されないだけです。
誰にも提案はありますか?