我正在创建一个应用程序来计算你按下按钮的次数,但是目前我的编码性能非常糟糕,我希望当达到5次点击时立即禁用按钮,但目前没有用,所以我想如果可能的话,你们是否可以帮助我改善它。
这是代码
public partial class MainPage : PhoneApplicationPage
{
private int count = 10;
private int tap = 0;
DispatcherTimer timerCountDown = new DispatcherTimer();
public MainPage()
{
InitializeComponent();
timerCountDown.Interval = new TimeSpan(0, 0, 1);
timerCountDown.Tick += new EventHandler(timerCountDown_Tick);
timerCountDown.Start();
}
//buttom counts
private void Button_Click(object sender, RoutedEventArgs e)
{
counter();
}
//timer
void timerCountDown_Tick(object sender, EventArgs e)
{
timer();
level();
}
//time remaning
private void timer()
{
txtTimerCount.Text = "Left " + count.ToString();
if (count > 0)
count--;
else
txtTimerCount.Text = "Times Up";
if (txtTimerCount.Text == "Times Up")
btntap.IsEnabled = false;
}
//counts clicks
private void counter()
{
tap++;
txtTimerTap.Text = "Tap Count: " + tap.ToString();
}
//level1
private void level()
{
if (tap == 5)
txtcomplet.Text = "Well done level completed";
if
(tap == 5)
btntap.IsEnabled = false;
}
答案 0 :(得分:1)
从level
方法而不是tick事件处理程序调用{{1}}方法:
counter
在//timer
void timerCountDown_Tick(object sender, EventArgs e)
{
timer();
}
//counts clicks
private void counter()
{
tap++;
txtTimerTap.Text = "Tap Count: " + tap.ToString();
level();
}
方法中,您可以使用代码块来避免双level
语句:
if