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函數一樣?
請幫助我解決上述問題。