wpf表单有定时器控件吗?

时间:2009-08-30 20:31:00

标签: wpf vb.net

wpf表单有定时器控件吗?

2 个答案:

答案 0 :(得分:5)

答案 1 :(得分:0)

WPF没有这样的Timer控件,但可以很容易地实现:

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Threading;

public partial class Timer : Label
{
    DispatcherTimer DTimer;
    int Seconds = 0, Minutes = 0;

    public Timer()
    {
        DTimer = new DispatcherTimer();
        DTimer.Interval = TimeSpan.FromSeconds(1);
        DTimer.Tick += new EventHandler(Tick);

        Content = "00:00";

        HorizontalContentAlignment = HorizontalAlignment.Center;
        VerticalContentAlignment = VerticalAlignment.Center;
    }

    void Tick(object sender, EventArgs e)
    {
        Seconds++;

        if (Seconds == 60)
        {
            Seconds = 0;
            Minutes++;
        }

        Content = string.Format("{0:D2}:{1:D2}", Minutes, Seconds);
    }

    public void Start() { DTimer.Start(); }

    public void Stop() { DTimer.Stop(); }

    public void Reset()
    {
        DTimer.Stop();

        Seconds = Minutes = 0;

        Content = "00:00";
    }
}