如何安排重复任务以在 Windows Phone 8 中倒数 [英] How to schedule a repeating task to count down a number in windows phone 8

查看:28
本文介绍了如何安排重复任务以在 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.

基本上它应该是这样工作的:用户进入这个屏幕,按下开始键,cont down timer 开始倒计时 - 每 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆