如何安排重复任务中的Windows Phone 8倒计时一些 [英] How to schedule a repeating task to count down a number in windows phone 8
问题描述
我需要只是,只要用户一个特定的屏幕上运行一个简单的任务。
在此屏幕上有向下定时器计数
I need to just have a simple task that runs as long as the user is on one specific screen. On this screen there is a count down timer.
我看着后台代理 - 但这似乎不是正确的做法。
I looked into Background Agents - but that seems not to be the right approach.
基本上它应该像这样的:用户进入这个屏幕,按下启动和续下来计时器开始倒计时 - 每30秒更新是完全确定
Basically it should work like this: The user goes to this one screen, presses start and the cont down timer starts to count down - every 30 seconds update is perfectly ok.
我应该怎样做这在WP8?
非常感谢!
How should I do this on WP8 ? Many thanks!
推荐答案
作为wkempf指出您应该使用DispatcherTimer。很简单,实际创建。像这样的东西(你有一个在你的XAML命名countText TextBlock的:
You should use a DispatcherTimer as wkempf points out. Pretty simple to create actually. Something like this (where you have a TextBlock named countText in your xaml:
public partial class MainPage : PhoneApplicationPage
{
private DispatcherTimer _timer;
private int _countdown;
// Constructor
public MainPage()
{
InitializeComponent();
_countdown = 100;
_timer = new DispatcherTimer();
_timer.Interval = TimeSpan.FromSeconds(1);
_timer.Tick += (s, e) => Tick();
_timer.Start();
}
private void Tick()
{
_countdown--;
if (_countdown == 0)
{
_timer.Stop();
}
countText.Text = _countdown.ToString();
}
}
这篇关于如何安排重复任务中的Windows Phone 8倒计时一些的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!