我正在开发Windows Phone 8游戏应用程序。
我需要添加倒计时function到我的应用程序。
就像应用程序启动一样 定时器值显示60,59,58 …. 0
当0达到显示消息“超时”。
我在谷歌search,但我不知道。 [可能是我的错误没有正确的search方式]
我尝试使用下面的代码显示date和时钟值,如[3/12/2014 6:22:10 PM]
C#代码
public SensorTwo() { InitializeComponent(); DispatcherTimer newTimer = new DispatcherTimer(); newTimer.Interval = TimeSpan.FromSeconds(1); newTimer.Tick += OnTimerTick; newTimer.Start(); } void OnTimerTick(Object sender, EventArgs args) { clock.Text = DateTime.Now.ToString(); }
XAML代码
<TextBlock Foreground="Red" FontSize="22" Name="clock" Width="350" Height="50" HorizontalAlignment="Center" VerticalAlignment="Bottom"></TextBlock>
任何一个帮助或指导我完成此function…
//make global declaration of a counter variable int counter =60; void OnTimerTick(Object sender, EventArgs args) { counter --; if(counter<0) { newTimer.Stop(); counter=60; } else { clock.Text =counter.ToString(); } }
声明一个计数器,一旦应用程序启动将重置
TimeSpan counter = new TimeSpan(0, 0, 60);
这在OnTimerTick()
counter -= TimeSpan.FromSeconds(1); clock.Text = counter.Seconds;
希望这会有所帮助
尝试这个
clock.Text = DateTime.Now.ToString(“ss”);