Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从30秒倒计时到0秒,用Xamarin在最后一秒显示2位小数#_C#_Xamarin.forms_Countdown - Fatal编程技术网

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

C# 从30秒倒计时到0秒,用Xamarin在最后一秒显示2位小数#,c#,xamarin.forms,countdown,C#,Xamarin.forms,Countdown,我对Xamarin很陌生。我想做的是一个短时钟。它需要从30秒倒计时到0秒,最后一秒需要显示2位小数。我还需要它在我单击BtnPause时暂停,如果我单击BtnStart,我希望它在我暂停它的确切时刻重新启动,因此必须暂停倒计时,并在计时器暂停的情况下启动,而不是重置 这是我的代码,请原谅我的错误和糟糕的编程技巧 public partial class MainPage : ContentPage { private static System.Timers.Tim

我对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();
                }
            });          
        }
    }
public分部类主页面:ContentPage
{
专用静态系统.Timers.Timer\u Timer;
私人双时钟=30;
公共主页()
{
初始化组件();
_定时器=新定时器();
}
已单击私有void BtnStart_(对象发送方,事件参数e)
{
运行计时器();
}
私有无效BtnPause_已单击(对象发送者,事件参数e)
{
PauseTimer();
}
私有void运行计时器()
{
如果(_shotClock<1.00)
{
_时间间隔=10;
}
其他的
{
_计时器。间隔=1000;
}
_timer.Start();
_timer.appeased+=OnTimedEvent;
}
私有void PauseTimer()
{
_timer.Stop();
}
私有void OnTimedEvent(对象源,System.Timers.ElapsedEventArgs e)
{
Device.beginInvokeMainThread(()=>{
如果(_shotClock>1.00)
{
_shotClock-=1.00;
_shotClock=数学圆(_shotClock,2);
lblTimer.Text=_shotClock.ToString();
} 
否则如果(_shotclock0.00)
{
_shotClock-=0.01;
_时间间隔=10;
_shotClock=数学圆(_shotClock,2);
lblTimer.Text=_shotClock.ToString();
}
其他的
{
lblTimer.Text=“0”;
_timer.Stop();
}
});          
}
}
出了什么问题:

我不相信它会精确计算秒数,特别是当我启动
Runtimer()
时,前几秒是关闭的,我不确定一次倒计时是一秒。当我调用
Pausetimer()
然后
Runtimer()
时,会跳过一秒钟


有更好的编码方法吗?

还没有编译这个,所以我不确定如果不做一些调整,它是否能工作,但我希望你能理解。基本上,您必须手动计算经过的时间。在您的案例中,秒数的减少可能是因为使用了Round()而不是Floor()

public分部类主页面:ContentPage
{
专用静态系统.Timers.Timer\u Timer;
私人双时钟=30;
私人日期时间(startTime);;
公共主页()
{
初始化组件();
_定时器=新定时器();
_时间间隔=10;
_timer.appeased+=OnTimedEvent;
}
已单击私有void BtnStart_(对象发送方,事件参数e)
{
_startTime=DateTime.UtcNow;
_timer.Start();
}
私有无效BtnPause_已单击(对象发送者,事件参数e)
{
_shotClock-=(DateTime.UtcNow-_startTime).TotalSeconds;
_shotClock=数学楼层(_shotClock);
_timer.Stop();
}        
私有void OnTimedEvent(对象源,System.Timers.ElapsedEventArgs e)
{
Device.beginInvokeMainThread(()=>{
var elapsedSinceBtnStartPressed=(DateTime.UtcNow-_startTime).TotalSeconds;
剩余变量=_shotClock-elapsedSincebtStartPressed;
如果(剩余>1.00)
{
lblTimer.Text=Math.Floor(剩余).ToString();
} 
否则,如果(剩余0.00)
{
lblTimer.Text=Math.Round(剩余,2.ToString();
}
其他的
{
lblTimer.Text=“0”;
_timer.Stop();
}
});          
}
}

如果您希望12.53显示为13而不是12,您可能还希望尝试替代Math.Floor()。

在公认答案的帮助下,我实现了基本功能:

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();
                }
            });
        }
    }
public分部类主页面:ContentPage
{
专用静态系统.Timers.Timer\u Timer;
私人双(U)挂钟;;
私人日期时间(startTime);;
公共主页()
{
初始化组件();
_定时器=新定时器();
_时间间隔=10;
_timer.appeased+=OnTimedEvent;
_shotClock=30.00;
}
已单击私有void BtnStart_(对象发送方,事件参数e)
{
_startTime=DateTime.UtcNow;
_timer.Start();
}
私有无效BtnPause_已单击(对象发送者,事件参数e)
{
_timer.Stop();
var elapsedSinceBtnPausePressed=(DateTime.UtcNow-_startTime).TotalSeconds;
_shotClock-=从按下BTN按钮起延时;
}
私有void OnTimedEvent(对象源,System.Timers.ElapsedEventArgs e)
{
var elapsedSinceBtnStartPressed=(DateTime.UtcNow-_startTime).TotalSeconds;
剩余变量=_shotClock-elapsedSincebtStartPressed;
Device.beginInvokeMainThread(()=>{
如果(剩余>1.00)
{
lblTimer.Text=Math.Floor(剩余).ToString();
}
否则,如果(剩余0.00)
{
lblTimer.Text=Math.Round(剩余,2.ToString();
}
其他的
{
lblTimer.Text=“0”;
_t
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();
                }
            });
        }
    }