2012-04-03 4 views
1

WPF의 타이머에 대해 궁금합니다. 위의 예제 코드에서 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 개의 개별 카운터 및 레이블 (각 시간 단위에 하나씩)을 사용 하시겠습니까? 또는이 문제를 해결할 수있는 더 좋은 방법은 무엇입니까?

+4

항상 내가 그 경우에 upvote에 – Dante1986

+0

... 싫어 레이블에 결과 문자열을 설정합니다 :) – thumbmunkeys

답변

1
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; 
    } 
} 
+0

그것을 사용하는 방법을 명확하지! – inside