在WP7中倒计时

时间:2011-07-30 13:17:04

标签: windows-phone-7

我想要倒数计时器开始滴答两个半小时。我使用调度程序计时器

尝试了这个

请找到以下代码。但它没有正确更新秒数。太快了。它一次打2秒而不是1秒

    private void DrawBlackout()
    {
        TextBlock videoText = new TextBlock();
        dt = new DispatcherTimer();
        dt.Interval = new TimeSpan(0,0,1); // 1 Seconds
        dt.Tick += new EventHandler(dt_Tick);
        dt.Start();


    }

    void dt_Tick(object sender, EventArgs e)
    {
        // do something

       milliseconds = milliseconds - 1000;
       label.Text = getTimeText(milliseconds / 1000);
       elapsed += 1000;

        //Debug.WriteLine(stopwatch.ElapsedMilliseconds + "\n");
        if (milliseconds <= 1000)
            dt.Stop();
     }



    String getTimeText(long secVal)
    {

        String timeString = "";
        int seconds = (int)(secVal % 60);
        int minutes = (int)((secVal / 60) % 60);
        int hours = (int)((secVal / (60 * 60)) % 60);
        if (hours <= 9)
            timeString = "0" + Convert.ToString(hours) + " : ";
        else
            timeString = Convert.ToString(hours) + " : ";
        if (minutes <= 9)
            timeString = timeString + "0" + Convert.ToString(minutes) + " : ";
        else
            timeString = timeString + Convert.ToString(minutes) + " : ";
        if (seconds <= 9)
            timeString = timeString + "0" + Convert.ToString(seconds) + " ";
        else
            timeString = timeString + Convert.ToString(seconds) + " ";
        return timeString;
    }

请帮助解决此问题。

1 个答案:

答案 0 :(得分:-1)

void DisTimer_Tick(object sender, EventArgs e)
        {
            timeRefresh++;
            if (sec > 0)
            {
                sec--;
            }
            else if (sec == 0 && min > 0)
            {
                sec = 59;
                min--;
            }
            else if (sec == 0 && min == 0 && house > 0)
            {
                sec = 59;
                min = 59;
                house--;
            }
            txtHouse.Text = house.ToString();
            txtMin.Text = min.ToString();
            txtSec.Text = sec.ToString();

            if (sec == 0 && min == 0 && house == 0)
            {
                DisTimer.Tick -= new EventHandler(DisTimer_Tick);
                getBuyer();
            }
            else if (timeRefresh == 60)
            {
                getBuyer();
                DisTimer.Tick -= new EventHandler(DisTimer_Tick);
            }
        }

我希望能帮到你!

Thongaduka(diadiem JSC)