我正在开发 Windows Phone 8 游戏应用程序。
我需要在我的应用中添加倒计时功能。
就像应用程序启动时一样。定时器值显示 60,59,58....0
当到达 0 时显示消息“超时”。
我在 Google 上搜索过,但我不知道。 [可能是我没有以正确的方式搜索的错误]
我尝试使用以下代码显示日期和时钟值,如 [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>
任何人帮助或指导我完成此功能......
最佳答案
//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();
}
}
关于c# - 使用带有 C# 的 XAML 为 Windows Phone 8 应用程序设置计时器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22352346/