0

我正在用 c# silverlight 编写扫雷游戏。
1. 我怎样才能在这个应用程序中添加一个计数器(只计算秒数)?
2. 当应用程序进入后台(中键、搜索键、来电等)时,如何停止计数器?
3. WP7正在关闭我的申请流程时,我该怎么办?例如将当前游戏保存到隔离存储。

4

1 回答 1

1

1)你需要使用定时器

        timer.Tick += new EventHandler(timer_Tick); // Everytime timer ticks, timer_Tick will be called
        timer.Interval = (1000) * (10);             // Timer will tick evert 10 seconds
        timer.Enabled = true;                       // Enable the timer
        timer.Start();                              // Start the time

void timer_Tick(object sender, EventArgs e)
        {
           //Do something
        }

2)您需要处理 OnNavigatedFrom 事件:

private void Application_Deactivated(object sender, DeactivatedEventArgs e)
{
    //Do something
}

3) 这里有 4 个有用的事件:

    // Code to execute when the application is launching (eg, from Start)
// This code will not execute when the application is reactivated
private void Application_Launching(object sender, LaunchingEventArgs e)
{
    //Do something
}

// Code to execute when the application is activated (brought to foreground)
// This code will not execute when the application is first launched
private void Application_Activated(object sender, ActivatedEventArgs e)
{
    //Do something
}

// Code to execute when the application is deactivated (sent to background)
// This code will not execute when the application is closing
private void Application_Deactivated(object sender, DeactivatedEventArgs e)
{
    //Do something
}

// Code to execute when the application is closing (eg, user hit Back)
// This code will not execute when the application is deactivated
private void Application_Closing(object sender, ClosingEventArgs e)
{
    //Do something
}

在这里您可以阅读有关处理此事件的更多信息:http: //msdn.microsoft.com/en-us/library/hh821027.aspx

于 2013-01-12T14:32:46.533 回答