c# - 从 30 秒倒计时到 0 秒,使用 Xamarin.Forms c# 在最后一秒显示 2 位小数
问题描述
我对 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()
一秒钟被跳过。
有没有更好的编码方法?
解决方案
还没有编译这个,所以我不确定它是否会在没有一些调整的情况下工作,但我希望你明白这个想法。基本上,您必须手动计算经过的时间。在您的情况下,秒数关闭可能是因为使用 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()。
推荐阅读
- angular - 以角度获取 GET http://localhost:4200/null 404(未找到)
- amazon-web-services - 无法将 docker 容器绑定到辅助接口
- javascript - 如何只运行一次javascript脚本而不让我的表单ajax调用运行
- angular - VSCode/tsconfig 路径问题 - 文件被拉入,但我在编辑器中得到“找不到模块”
- swift - 通过该特定单元格中的按钮删除 tableviewcell
- generator - 你能交错来自多个文件的 tf.data 数据集吗?
- python - 删除两个网格之间的空间 - Tkinter
- python - 如何为数据框中的两个特定列选择两个数字之间的值以绘制为二维图
- python - 无法将 Twilio Python SDK 响应对象序列化为简单的 dict 对象
- javascript - 如何访问Nodejs中两个数组内的对象?