WPF计时器倒计时

本文关键字:倒计时 计时器 WPF | 更新日期: 2023-09-27 18:29:58

我想知道WPF中的计时器。

我基本上知道的是如何制作一个简单的倒计时计时器(标签)倒计时,就像下面的代码:

private void buttonStartOne_Click(object sender, RoutedEventArgs e)
{
    counterOne = new DispatcherTimer();
    counterOne.Tick += new EventHandler(counterOne_Tick);
    counterOne.Interval = new TimeSpan(0, 0, 1);
    counterOneTime = 10;
    counterOne.Start();
}
private void counterOne_Tick(object sender, EventArgs e)
{
// code goes here
    if (counterOneTime > 0)
    {
        counterOneTime--;
        labelCounterOne.Content = counterOneTime + "s";
    }
    else
        counterOne.Stop();
}

在上面的示例代码中,倒计时只有10秒。我想要的,但不知道的是,我应该如何把它变成:HH:mm:ss,然后让它倒计时。你会用3个单独的计数器和标签(每个时间单位一个)来做到这一点吗?或者应该有什么更好的方法来解决这个问题?

WPF计时器倒计时

您可以使用ToString(字符串格式)方法将Timespan转换为字符串。在标签上设置结果字符串

public class TimeController
{
    private static readonly TimeSpan TimeSpan = new TimeSpan(0, 0, 1);
    private static int _time;
    protected static readonly DispatcherTimer Timer = new DispatcherTimer();
    protected static readonly DispatcherTimer BeeperTimer = new DispatcherTimer();
    protected static readonly Stopwatch StopWatch = new Stopwatch();
    protected static Label TimerLabel;
    protected static Button StartButton;
    internal static int Time { get { return _time; } set { _time = value; ExtractAndUpdate(); } }
    internal static bool Countdown { get; set; }
    /// <summary>
    /// Static constructor
    /// </summary>
    static TimeController()
    {
        BeeperTimer.Interval = TimeSpan;
        BeeperTimer.Tick += BeeperTick;
        Timer.Interval = TimeSpan;
        Timer.Tick += TimerTick;
    }
    /// <summary>
    /// Timer tick event method
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private static void TimerTick(object sender, EventArgs e)
    {
        if (Countdown)
            if (Time > 0)
            {
                ExtractAndUpdate();
                Time -= 1;
            }
            else
            {
                StopRunning();
                BeeperTimer.Start();
            }
        else
            ExtractAndUpdate();
    }
    /// <summary>
    /// Start timer and stopwatch
    /// </summary>
    protected static void StartRunning()
    {
        Timer.Start();
        StopWatch.Start();
        StartButton.Content = Labels.Pause;
    }
    /// <summary>
    /// Stop timer and stopwatch
    /// </summary>
    protected static void StopRunning()
    {
        Timer.Stop();
        StopWatch.Stop();
        StartButton.Content = Labels.Start;
    }
    /// <summary>
    /// Beeper event method and label blinking
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private static void BeeperTick(object sender, EventArgs e)
    {
        TimerLabel.Visibility = TimerLabel.Visibility.Equals(Visibility.Hidden) ? Visibility.Visible : Visibility.Hidden;
        Console.Beep();
    }
    /// <summary>
    /// Extract time and update label
    /// </summary>
    private static void ExtractAndUpdate()
    {
        var elapsed = Countdown ? ConvertToTimeSpan() : StopWatch.Elapsed;
        UpdateTimeLabel(elapsed);
    }
    /// <summary>
    /// Convert int to TimeSpan
    /// </summary>
    /// <returns></returns>
    internal static TimeSpan ConvertToTimeSpan()
    {
        var hours = Time / 3600;
        var minutes = (Time % 3600) / 60;
        var seconds = Time % 60;
        return new TimeSpan(hours, minutes, seconds);
    }
    /// <summary>
    /// Update label with data and change color
    /// </summary>
    /// <param name="elapsed"></param>
    protected static void UpdateTimeLabel(TimeSpan elapsed)
    {
        TimerLabel.Foreground = Brushes.Black;
        var time = String.Format(CultureInfo.CurrentCulture, "{0:00h} {1:00m} {2:00s}", elapsed.Hours, elapsed.Minutes, elapsed.Seconds);
        if (Countdown && elapsed.TotalMinutes < 1)
            TimerLabel.Foreground = Brushes.Red;
        TimerLabel.Content = time;
    }
}