如何为我的Windows 8应用程序引入操作延迟

时间:2015-03-06 18:24:27

标签: c# windows-store-apps

我没有成功地在网上搜索我想要完成的事情(可能是因为我不知道我需要什么!)。到目前为止,我已经开发了一个问答游戏,它有几种不同的模式和每个问题60秒的计时器。我唯一的问题是,我不知道如何在屏幕上延迟的问题之间引入2-3秒的延迟。现在,如果用户得到的问题是对还是错,下一个问题会立即出现在屏幕上。我想要发生的是屏幕变成空白(我只是用[xyx.Text =“”;]来清理文本块,除了显示“正确!”或“错误!”的文本块。大约3秒钟。

2-3秒后,程序将继续正常,随机选择要在屏幕上显示的问题。为清楚起见,这是我正在使用的当前代码

public sealed partial class QuickPage : Page
{
 DispatcherTimer timeLeft = new Dispatcher();
 int timesTicked = 60;

public void CountDown()
{
    timeLeft.Tick += timeLeft_Tick;
    timeLeft.Interval = new TimeSpan(0,0,0,1);
    timeLeft.Start();
}

public void timeLeft_Tick(object sender, object e)
{
    lblTime.Text = timesTicked.ToString();

    if (timesTicked > 0)
    {
        timesTicked--;
    }
    else
    {
        timeLeft.Stop();
        lblTime.Text = "Times Up";
    }
}

这是一个线程,我在那里得到了我用于倒数计时器的DispatcherTimer的帮助:Help with DispatcherTimer

1 个答案:

答案 0 :(得分:0)

延迟5秒的示例:

DispatcherTimer timer = new DispatcherTimer();

// Call this method after the 60 seconds countdown.
public void Start_timer()
{        
    timer.Tick += timer_Tick;
    timer.Interval = new TimeSpan(0, 0, 5);
    bool enabled = timer.IsEnabled;

    // Check and show answer is correct or wrong

    timer.Start();       
}

void timer_Tick(object sender, object e)
{
    this.Visibility = System.Windows.Visibility.Visible;
    (sender as DispatcherTimer).Stop(); // Or you can just call timer.Stop() if the timer is a global variable.

    // Clear screen, go to the next question
}