1
我是C#和WPF的新手。我要做到以下几点:显示标签和移动形状
显示几个标签一前一后整整5秒后,
完成上述后,我要在画布上移动形状与时间约十倍每次移动之间的间隔为5秒,
完成上述步骤但时间间隔仅为2秒。
下面是代码:
DispatcherTimer timer2 = new DispatcherTimer();
float timerTime = 10;
Label timerlabel = new Label();
private void Window_Loaded(object sender, RoutedEventArgs e)
{
lbl.Content = "test";
startDisplay("hello!!");
startDisplay("bye");
Shapemove(1);
}
private void startDisplay(string st)
{
DispatcherTimer timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromSeconds(5);
timer.Start();
timer.Tick += (s, e) =>
{
lbl.Content = st;
};
}
private void Shapemove(int i)
{
timer2.Interval = new TimeSpan(0, 0, 2);
timer2.Tick += new EventHandler(timer2_Tick);
timer2.Start();
}
void timer2_Tick(object sender, EventArgs e)
{
Random rand = new Random();
if (timerTime > 0)
{
canvas1.Children.Remove(timerlabel);
timerTime--;
canvas1.Children.Add(timerlabel);
timerlabel.FontSize = 20;
timerlabel.Content = timerTime + "s";
Canvas.SetLeft(rectangle1, rand.Next(640));
Canvas.SetTop(rectangle1, rand.Next(480));
}
else
{
timer2.Stop();
}
}
但问题是以上:
两个计时器和Timer2在同一时间掀起。
标签不会一个接一个地显示 - 测试出现,5秒钟后再见,你好永远不会出现!
有没有办法重置计时器并将它们作为函数重复调用,就像上面提到的Shapemove或startDisplay函数一样?
请帮助我解决上述问题。