我在這裏有一個倒數計時器,有沒有反正可以改善它,使它看起來更好顯示01:09而不是1:9,並且00: 09而不是00:9以下是code.I需要一些指導如何做到這一點。如何改進WPF倒數計時器的時間格式
private int time = 120;
private System.Windows.Threading.DispatcherTimer Timer;
public MainWindow()
{
InitializeComponent();
Timer = new System.Windows.Threading.DispatcherTimer();
Timer.Interval = new TimeSpan(0, 0, 1);
Timer.Tick += Timer_tick;
Timer.Start();
}
void Timer_tick(object sender, EventArgs e)
{
if (time > 0)
{
if (time <= 10)
{
if (time % 2 == 0)
{
tbTime.Foreground = Brushes.Red;
}
else
{
tbTime.Foreground = Brushes.White;
}
time--;
tbTime.Text = string.Format("00:0{0}:0{1}", time/60, time % 60);
}
else
{
time--;
tbTime.Text = string.Format("00:0{0}:{1}", time/60, time % 60);
}
}
else
{
Timer.Stop();
}
}
看看'DateTime'和'ToString',他們會幫助你很大 – Ian