如何制作倒数计时器?

时间:2017-04-24 15:16:27

标签: c# .net visual-studio uwp uwp-xaml

您好我正在编写记分板UWP应用程序,我想知道如何在计时器后面制作代码。因为它是一个篮球记分牌,它有2个时钟,一个只有秒(射击时钟)和其他管理分钟和秒。 所以,我想知道是否有一种简单的方法可以在UWP中进行这种倒计时。

我刚刚发现了这个,但它没有计算下来:

    private void stopwatch_Tapped(object sender, TappedRoutedEventArgs e)
    {
        if (_stopwatch.IsRunning)
        {
            _stopwatch.Stop();
            _timer.Dispose();
        }
        else
        {
            _stopwatch.Start();
            _timer = new Timer(updateTime, null, (int)TimeSpan.FromMinutes(1).TotalMinutes, Timeout.Infinite);
        }

    }

    private async void updateTime(object state)
    {
        await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
            () =>
                {

                    stopwatchLbl.Text = String.Format("{0:00}:{1:00}:{2:00}", _stopwatch.Elapsed.TotalMinutes, _stopwatch.Elapsed.TotalSeconds, _stopwatch.Elapsed.TotalMilliseconds / 10);

                    //stopwatchLbl.Text = "00:00:00";

                }
            );

    }

2 个答案:

答案 0 :(得分:0)

尝试此代码,我希望这会对您有所帮助:)。

internal class countDownTimer
    {
        public int enlapsedTime;
        private DispatcherTimer dispatch;

        public delegate void MyCallback();
        public delegate void MyCallback2(int value);
        public MyCallback OnStartTime;
        public MyCallback OnStopTime;
        public MyCallback OnEndTime;
        public MyCallback2 OnCountTime;

        public countDownTimer()
        {
            Debug.WriteLine("StopWatch init");
            enlapsedTime = 0;
            dispatch = new DispatcherTimer();
            dispatch.Interval = new TimeSpan(0, 0, 1);
            dispatch.Tick += timer_Tick;
        }

        private void timer_Tick(object sender, object e)
        {
            enlapsedTime--;
            Debug.WriteLine(enlapsedTime);

            if (OnCountTime != null) OnCountTime(enlapsedTime);

            if (enlapsedTime < 0)
            {
                enlapsedTime = 0;
                dispatch.Stop();
                if (OnEndTime != null) OnEndTime();
            }
        }

        public void Start()
        {
            dispatch.Start();
            if (OnStartTime != null) OnStartTime();
            Debug.WriteLine("iniciar contador");
        }

        public void Stop()
        {
            dispatch.Stop();
            if (OnStopTime != null) OnStopTime();
            Debug.WriteLine("parar contador");
        }

        public bool IsEnabled
        {
            get
            {
                return dispatch.IsEnabled;
            }
        }

    }

答案 1 :(得分:0)

You may use the UWPHelper.Utilities.ThreadPoolTimer from my NuGet package UWPHelper. You will need to check the Include pre-release checkbox in NuGet Package Manager to be able to download it.

My ThreadPoolTimer is a wrapper class for System.Threading.Timer and it works similarly to the DispatcherTimer however it runs on the ThreadPool, not on the UI thread.

using UWPHelper.Utilities;

// TimeSpan indicates the interval of the timer
ThreadPoolTimer timer = new ThreadPoolTimer(TimeSpan.FromSeconds(1));
timer.Tick += OnTick;

void OnTick(object sender, EventArgs e)
{
    // Method invoked on Tick - every second in this scenario
} 

// To start the timer
timer.Start();

// To stop the timer
timer.Stop();