2012-05-08 2 views
1

3 개의 버튼 (Button1, Button2 및 StartButton)과 타이머가있는 win8 앱을 만들고 있습니다. Button1 및 Button2는 사용할 수 없습니다. StartButton을 클릭하면 Button1이 활성화되고 20 초 내에 클릭 수가 계산되어 텍스트 블록 1에 표시됩니다. 타이머가 끝나면 Button1이 비활성화되고 Button2가 활성화되고 클릭 수가 계산되어 textblock2에 표시됩니다. 내 문제는 Button1이 아니라 Button2가 아니라 타이머가 올바르게 틱된다는 것입니다. button2가 활성화되면 타이머가 빨라집니다. 누군가 나를 도울 수 있습니까? 내 코드는 다음과 같습니다 :Win8 앱에서 타이머 카운팅

private int count1=0; 
    private int count2=0; 
    private int clickCounter = 0; 
    private int timeLeft; 
    private DispatcherTimer timer; 

    private void StartTimer() 
    { 
     if (this.timer != null) 
     { 
      this.StopTimer(); 
     } 
     DispatcherTimer timer = new DispatcherTimer(); 
     timer.Interval = new TimeSpan(0,0,0,1); 
     timer.Tick += timer_Tick; 
     timer.Start(); 

    } 

    private void StopTimer() 
    { 
     if (this.timer != null) 
     { 
      this.timer.Stop(); 
      this.timer = null; 
     } 
    } 

    public void timer_Tick(object sender, object args) 
    { 

     if (timeLeft > 0) 
     { 
      timeLeft = timeLeft - 1; 
      timerTextBlock.Text = Convert.ToString(timeLeft); 
      this.StartButton.IsEnabled = false; 
     } 
     else 
     { 
      StopTimer(); 
      if (clickCounter==2) 
      { 
       ShowResult(); 
       this.Button2.IsEnabled = false; 
       this.StartButton.IsEnabled = false; 
      } 
      else 
      { 
       myMsg.Text = "Time's up!"; 
       this.Button1.IsEnabled = false; 
       this.StartButton.IsEnabled = true; 
      } 
     } 
    } 

    private void Button1_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e) 
    { 
     // TODO: Add event handler implementation here. 
     count1++; 
     this.textblock1.Text=count1.ToString(); 

    } 

    private void Button2_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e) 
    { 
     count2++; 
     this.textblock2.Text=count2.ToString(); 
    } 
    public void ResetTimer() 
    { 
      timeLeft = 20; 
    } 
    private void StartButton_Click(object sender, RoutedEventArgs e) 
    { 
     clickCounter++; 
     if (textblock1.Text == "0") 
     { 
      ResetTimer(); 
      StartTimer(); 
      this.Button1.IsEnabled = true; 
     } 
     else 
     { 
      ResetTimer(); 
      StartTimer(); 
      this.Button2.IsEnabled = true; 
     } 
    } 

답변

3

당신이 StartTimer 방법 timer.Tick += timer_Tick;이 실행될 때 호출 할 때마다. 즉, StartTimer을 두 번째 호출하면 모든 틱에서 두 개의 이벤트가 호출됩니다. 코드를 다음과 같이 분할하십시오.

private void InitTimer() 
{ 
    timer = new DispatcherTimer(); 
    timer.Interval = new TimeSpan(0,0,0,1); 
    timer.Tick += timer_Tick; 
} 

private void StartTimer() 
{ 
    timer.Start(); 
} 

및 InitTimer 만 호출하십시오. 또 다른 옵션은 코드 timer.Tick -= timer_Tick; 으로 if (this.timer != null)에 이벤트 등록을 취소하는 것입니다. 두 번째로 알 수있는 것은 명명 충돌입니다. StartTimer 메소드에 개인 전역 변수 타이머와 하나의 변수 타이머가 있습니다.

// 편집 : 전체 업데이트 된 코드 :

private int count1=0; 
private int count2=0; 
private int clickCounter = 0; 
private int timeLeft; 
private DispatcherTimer timer; 

private void StartTimer() { 
    if (timer == null) { 
     timer = new DispatcherTimer(); 
     timer.Interval = new TimeSpan(0,0,0,1); 
     timer.Tick += timer_Tick; 
    } 
    timer.Stop(); 
    timeLeft = 20; 
    timer.Start(); 
} 

public void timer_Tick(object sender, object args) { 
    if (timeLeft > 0) { 
     timeLeft = timeLeft - 1; 
     timerTextBlock.Text = Convert.ToString(timeLeft); 
    } else { 
     timer.Stop(); 
     if (clickCounter==2) { 
      ShowResult(); 
      Button2.IsEnabled = false; 
      StartButton.IsEnabled = false; 
     } else { 
      myMsg.Text = "Time's up!"; 
      Button1.IsEnabled = false; 
      StartButton.IsEnabled = true; 
     } 
    } 
} 

private void Button1_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e) { 
    count1++; 
    textblock1.Text=count1.ToString(); 
} 

private void Button2_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e) { 
    count2++; 
    textblock2.Text=count2.ToString(); 
} 

private void StartButton_Click(object sender, RoutedEventArgs e) { 
    clickCounter++; 
    StartButton.IsEnabled = false; 
    if (textblock1.Text == "0"){ 
     Button1.IsEnabled = true; 
     StartTimer(); 
    } else { 
     Button2.IsEnabled = true; 
     StartTimer(); 
    } 
} 
+0

은 내가 StartTimer는() 내에서 타이머 변수를 제거해야하고 {이 경우 (this.timer = NULL을!) 넣어 두 번째 옵션을 따르도록했습니다. timer.Tick - = timer_Tick; this.StopTimer();} 맞습니까? – Shan

+0

첫 번째 방법을 사용한 경우 InitTimer()를 어디에 호출해야합니까? – Shan

+0

최상은 MainPage의 PageLoaded 이벤트에서 호출하는 것이 좋습니다. –