使用 XAML 和 C# 为 Windows phone 8 应用程序设置计时器 [英] Set Timer For Windows phone 8 App Using XAML With C#
本文介绍了使用 XAML 和 C# 为 Windows phone 8 应用程序设置计时器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在开发 Windows Phone 8 游戏应用.
I'm developing windows phone 8 game app.
我需要在我的应用中添加倒计时功能.
I need To add countdown time functionality into my app.
就像应用程序启动时一样.定时器值显示 60,59,58....0
like when application start. Timer Value Show 60,59,58....0
到达 0 时显示消息超时".
When 0 Reach Show Msg "Time Out".
我在谷歌搜索过,但我不知道.[可能是我的错误没有以正确的方式搜索]
I searched in Google but i don't have any idea. [may be it's my mistake not searching in correct way]
我尝试使用以下代码显示日期和时钟值,例如 [3/12/2014 6:22:10 PM]
I try with following code it's show the date and clock values like [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>
任何人帮助或指导我完成此功能...
any One help or Guid me To Complete This Functionality...
推荐答案
//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();
}
}
这篇关于使用 XAML 和 C# 为 Windows phone 8 应用程序设置计时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文