2012-05-23 51 views
1

我是C#和WPF的新手。我要做到以下几点:显示标签和移动形状

  1. 显示几个标签一前一后整整5秒后,

  2. 完成上述后,我要在画布上移动形状与时间约十倍每次移动之间的间隔为5秒,

  3. 完成上述步骤但时间间隔仅为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(); 
     } 
    } 

但问题是以上:

  1. 两个计时器和Timer2在同一时间掀起。

  2. 标签不会一个接一个地显示 - 测试出现,5秒钟后再见,你好永远不会出现!

  3. 有没有办法重置计时器并将它们作为函数重复调用,就像上面提到的Shapemove或startDisplay函数一样?

请帮助我解决上述问题。

回答

1

请勿使用计时器。改为使用StoryBoard。

在故事板中,您可以安排操纵控件的可见性,不透明度,位置,...任何(依赖性)属性的动画。

See Animations in this tutorial