从30秒倒计时到0秒,使用Xamarin.Forms c#在最后一秒显示2位小数

时间:2019-11-29 13:07:42

标签: c# xamarin.forms countdown

我对Xamarin非常陌生。我想做的是拍钟。它需要从30秒倒数到0秒,并且在最后一秒需要显示2个小数。当我单击BtnPause时,我还需要将其暂停。如果我单击BtnStart,我希望它在暂停的那一刻重新启动,因此倒计时必须暂停并在计时器暂停时开始,而不是重置。

这是我的代码,请原谅我的错误和不良的编程技能。

  public partial class MainPage : ContentPage
    {
        private static System.Timers.Timer _timer;
        private double _shotClock = 30;

        public MainPage()
        {
            InitializeComponent();
            _timer = new Timer();

        }

        private void BtnStart_Clicked(object sender, EventArgs e)
        {
            RunTimer();

        }
        private void BtnPause_Clicked(object sender, EventArgs e)
        {
            PauseTimer();
        }

        private void RunTimer()
        {
            if (_shotClock < 1.00)
            {
                _timer.Interval = 10;
            }
            else 
            {
                _timer.Interval = 1000;
            }

            _timer.Start();
            _timer.Elapsed += OnTimedEvent;          

        }

        private void PauseTimer()
        {
            _timer.Stop();
        }

        private void OnTimedEvent(Object source, System.Timers.ElapsedEventArgs e)
        {
            Device.BeginInvokeOnMainThread(() => {

                if (_shotClock > 1.00)
                {

                    _shotClock -= 1.00;
                    _shotClock = Math.Round(_shotClock, 2);
                    lblTimer.Text = _shotClock.ToString();
                } 
                else if (_shotClock <= 1.00 && _shotClock > 0.00)
                {
                    _shotClock -= 0.01;
                    _timer.Interval = 10;
                    _shotClock = Math.Round(_shotClock, 2);
                    lblTimer.Text = _shotClock.ToString();
                }
                else
                {
                    lblTimer.Text = "0";
                    _timer.Stop();
                }
            });          
        }
    }

出了什么问题:

我不认为这是精确的秒数,尤其是当我启用Runtimer()时,前几秒已关闭,并且我不确定一个倒数是一秒。当我调用Pausetimer()然后调用Runtimer()时,将跳过一秒钟。

是否有更好的方法对此进行编码?

2 个答案:

答案 0 :(得分:1)

还没有编译这个,所以我不确定它是否可以在不作任何调整的情况下工作,但是我希望您能理解。基本上,您必须手动计算经过时间。在您的情况下,关闭秒数可能是因为使用Round()而不是Floor()

public partial class MainPage : ContentPage
{
    private static System.Timers.Timer _timer;
    private double _shotClock = 30;
    private DateTime _startTime;

    public MainPage()
    {
        InitializeComponent();
        _timer = new Timer();
        _timer.Interval = 10;
        _timer.Elapsed += OnTimedEvent;  
    }

    private void BtnStart_Clicked(object sender, EventArgs e)
    {
        _startTime = DateTime.UtcNow;
        _timer.Start();
    }
    private void BtnPause_Clicked(object sender, EventArgs e)
    {
        _shotClock -= (DateTime.UtcNow - _startTime).TotalSeconds;
        _shotClock = Math.Floor(_shotClock);
        _timer.Stop();
    }        

    private void OnTimedEvent(Object source, System.Timers.ElapsedEventArgs e)
    {
        Device.BeginInvokeOnMainThread(() => {
            var elapsedSinceBtnStartPressed = (DateTime.UtcNow - _startTime).TotalSeconds;
            var remaining = _shotClock - elapsedSinceBtnStartPressed;

            if (remaining > 1.00)
            {
                lblTimer.Text = Math.Floor(remaining).ToString();
            } 
            else if (remaining <= 1.00 && remaining > 0.00)
            {
                lblTimer.Text = Math.Round(remaining, 2).ToString();
            }
            else
            {
                lblTimer.Text = "0";
                _timer.Stop();
            }
        });          
    }
}

如果希望将12.53显示为13而不是12,则可能还需要尝试Math.Ceiling()而不是Math.Floor()。

答案 1 :(得分:0)

在被接受的答案的帮助下,我掌握了基础知识:

public partial class MainPage : ContentPage
    {
        private static System.Timers.Timer _timer;
        private double _shotClock;
        private DateTime _startTime;



        public MainPage()
        {
            InitializeComponent();
            _timer = new Timer();
            _timer.Interval = 10;
            _timer.Elapsed += OnTimedEvent;
            _shotClock = 30.00;
        }

        private void BtnStart_Clicked(object sender, EventArgs e)
        {
            _startTime = DateTime.UtcNow;     
            _timer.Start();
        }

        private void BtnPause_Clicked(object sender, EventArgs e)
        {


            _timer.Stop();
            var elapsedSinceBtnPausePressed = (DateTime.UtcNow - _startTime).TotalSeconds;
            _shotClock -= elapsedSinceBtnPausePressed;


        }

        private void OnTimedEvent(Object source, System.Timers.ElapsedEventArgs e)
        {
            var elapsedSinceBtnStartPressed = (DateTime.UtcNow - _startTime).TotalSeconds;
            var remaining = _shotClock - elapsedSinceBtnStartPressed;

            Device.BeginInvokeOnMainThread(() => {


                if (remaining > 1.00)
                {
                    lblTimer.Text = Math.Floor(remaining).ToString();
                }
                else if (remaining <= 1.00 && remaining > 0.00)
                {
                    lblTimer.Text = Math.Round(remaining, 2).ToString();
                }
                else
                {
                    lblTimer.Text = "0";
                    _timer.Stop();
                }
            });
        }
    }
相关问题