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;
}
}
은 내가 StartTimer는() 내에서 타이머 변수를 제거해야하고 {이 경우 (this.timer = NULL을!) 넣어 두 번째 옵션을 따르도록했습니다. timer.Tick - = timer_Tick; this.StopTimer();} 맞습니까? – Shan
첫 번째 방법을 사용한 경우 InitTimer()를 어디에 호출해야합니까? – Shan
최상은 MainPage의 PageLoaded 이벤트에서 호출하는 것이 좋습니다. –