简体   繁体   English

延迟Windows 8应用程序倒数计时器C#

[英]Delay windows 8 app countdown timer c#

I have a windows 8 game app that uses a timer for each level and I am trying to the timer 3 seconds. 我有一个Windows 8游戏应用程序,每个级别使用一个计时器,并且我尝试将计时器设置为3秒。

The code I have to start the timer works but I can't seem to delay the timer so that I display words to count it down. 我必须启动计时器的代码可以正常工作,但是我似乎无法延迟计时器,因此我无法显示文字以将其倒计时。

Here is the code: 这是代码:

private async void timer_Tick(object sender, object e)
        {
            await
                Time.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Low,
                    () =>
                    { Time.Text = string.Format("{0}:{1}", (Counter/60), (Counter%60).ToString().PadLeft(2, ' ')); });
            Counter--;
            await Task.Delay(3000);}}

so I put async there because I thought I should put await Task.Delay(3000); 所以我把异步放在那里,因为我认为我应该等待Task.Delay(3000); after counter to delay the timer but it doesn't work. 后计数器延迟计时器,但它不起作用。 I do not want to the WinRT Xaml toolkit countdown timer because I don't want the animation there. 我不想使用WinRT Xaml工具包的倒数计时器,因为我不想在那里播放动画。

Any suggestions on what I am doing wrong would be great! 关于我在做什么错的任何建议都很棒!

So presumably you want to update the display every second, to get this countdown? 因此,假设您想每秒更新一次显示,以获得此倒计时? But if I've understood, for some reason you want to delay everything by 3 seconds. 但是,据我了解,出于某种原因,您希望将所有内容延迟3秒。

If you want things to happen 3 seconds later than they're currently happening, the obvious solution is to program the timer so it calls you when you actually want it to: 如果您希望事情比当前发生的时间晚3秒发生,那么显而易见的解决方案是对计时器进行编程,使其在实际需要时调用您:

private DispatcherTimer t = new DispatcherTimer();
private int Counter = 120;

public MainPage()
{
    InitializeComponent();

    t.Interval = TimeSpan.FromSeconds(4);
    t.Tick += timer_Tick;
    t.Start();
}

private void timer_Tick(object sender, object o)
{
    t.Interval = TimeSpan.FromSeconds(1);
    Time.Text = string.Format("{0}:{1}", (Counter / 60), (Counter % 60).ToString().PadLeft(2, ' '));
    Counter--;
}

That makes the first tick take 4 seconds to arrive, and then adjusts the interval to 1 second. 这使第一个滴答声需要4秒钟才能到达,然后将间隔调整为1秒。 So you'll get ticks spaced at 1 second intervals, but everything will happen 3 seconds later than it otherwise would. 因此,您将获得间隔为1秒的滴答声,但是一切都会比原先晚3秒。 (If set the tick interval to 1 initially, then the first tick would take 1 second to arrive, which is why you need a delay of 4 seconds initially - an initial tick of 3 would only delay things by 2 seconds.) (如果最初将刻度间隔设置为1,则第一个刻度需要1秒才能到达,这就是为什么您最初需要延迟4秒的原因-初始刻度3只会使事物延迟2秒。)

However, if you need to do some things immediately, and some with a delay, one obvious way to do that would be just to adjust the count in your handler by 3 seconds: 但是,如果您需要立即执行某些操作,而有些则需要延迟,那么一种明显的执行方法是将处理程序中的计数调整3秒:

private const int LevelMaxTime = 120;
private DispatcherTimer t = new DispatcherTimer();
private int Counter = LevelMaxTime;

public MainPage()
{
    InitializeComponent();

    t.Interval = TimeSpan.FromSeconds(1);
    t.Tick += timer_Tick;
    t.Start();
}

private void timer_Tick(object sender, object o)
{
    // Do the undelayed work here, whatever that is...

    // Next, we do the delayed work, if there is any yet.
    int effectiveCount = Counter + 3;
    if (effectiveCount <= LevelMaxTime)
    {
        Time.Text = string.Format("{0}:{1}", (effectiveCount / 60), (effectiveCount % 60).ToString().PadLeft(2, ' '));
    }
    Counter--;
}

This just takes the effective current time to be 3 seconds before what Current says it is, thus delaying everything by 3 seconds. 这仅需要有效电流时间比Current所说的要早3秒钟,因此将所有内容延迟了3秒钟。

You could do something more like your original code: 您可以做一些更像您原始代码的操作:

private async void timer_Tick(object sender, object o)
{
    // Wait for 3 seconds...for some reason
    await Task.Delay(TimeSpan.FromSeconds(3));
    Time.Text = string.Format("{0}:{1}", (Counter / 60), (Counter % 60).ToString().PadLeft(2, ' '));
    Counter--;
}

That's closer in spirit to what you wrote (as far as I can tell), only it works, but it seems unnecessarily convoluted. 从精神上讲,这与您写的内容(据我所知)更接近,只有它起作用了,但似乎不必要地令人费解。 Why not just program the timer to call you at the right time, rather than getting callbacks at the wrong time and then trying to compensate? 为什么不对计时器进行编程以在正确的时间调用您,而不是在错误的时间获取回调然后尝试进行补偿呢? It's a timer. 这是一个计时器。 It'll call you when you tell it to! 告诉您时,它将给您打电话!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM