【发布时间】:2019-02-10 08:53:48
【问题描述】:
我正在尝试以 2 秒的时间间隔更改图片。它应该只有一次,而不是无限的时间圈。
我用谷歌搜索了各种替代词,但找不到。就像 thread.sleep(2000) 不起作用,因为它冻结了界面。
public partial class Window1 : Window
{
private static System.Timers.Timer aTimer;
public void RemoveImage()
{
Image.Source = new BitmapImage(new Uri("path to image 2"));
SetTimer();
}
private void SetTimer()
{
// Create a timer with a two second interval.
aTimer = new System.Timers.Timer(2000);
// Hook up the Elapsed event for the timer.
aTimer.Elapsed += OnTimedEvent;
aTimer.AutoReset = true;
aTimer.Enabled = true;
}
private void OnTimedEvent(Object source, ElapsedEventArgs e)
{
Image.Source = new BitmapImage(new Uri("path to image 3"));
}
XAML 代码
<Window
<Image Source="path to image 1" Grid.Row="1" Grid.Column="8"
Name="Image" Stretch="None" HorizontalAlignment="Center"
VerticalAlignment="Center"></Image>
</Grid>
</Window>
使用此代码,我可以看到第二张图片,但是当它到达最后一张图片时,您会收到错误 System.InvalidOperationException for image 3。希望您能帮助我解决任何问题
【问题讨论】: