0

我正在制作一个应用程序来计算您按下按钮的次数,但目前我的编码性能非常糟糕,我想在点击 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;

    }
4

1 回答 1

1

level从方法调用counter方法而不是 tick 事件处理程序:

//timer
void timerCountDown_Tick(object sender, EventArgs e)
{
    timer();
}

//counts clicks
private void counter() 
{
    tap++;
    txtTimerTap.Text = "Tap Count: " + tap.ToString();
    level();
}

编辑:

在该level方法中,您可以使用代码块来避免双重if语句:

private void level()
{
    if (tap == 5) {
        txtcomplet.Text = "Well done level completed";
        btntap.IsEnabled = false;
    }
}
于 2013-09-21T22:44:00.350 回答