【发布时间】:2021-10-08 12:42:17
【问题描述】:
我有一个包含倒数计时器的 WPF 应用程序,我被它的格式化部分所困扰,我几乎没有编程经验,这是我第一次使用 c#。我想使用 DispatchTimer 从 15 分钟 开始倒计时,但到目前为止,我的计时器只从 15 秒 开始倒计时>,有什么想法吗?
到目前为止我的倒计时:
public partial class MainWindow : Window
{
private int time = 15;
private DispatcherTimer Timer;
public MainWindow()
{
InitializeComponent();
Timer = new DispatcherTimer();
Timer.Interval = new TimeSpan(0,0,1);
Timer.Tick += Timer_Tick;
Timer.Start();
}
void Timer_Tick(object sender, EventArgs e) {
if (time > 0)
{
time--;
TBCountDown.Text = string.Format("{0}:{1}", time / 60, time % 60);
}
else {
Timer.Stop();
}
}
输出如下:
【问题讨论】: