【发布时间】:2013-01-12 14:26:54
【问题描述】:
我正在用 c#silverlight 编写扫雷游戏。
1. 我怎样才能在这个应用程序中添加一个计数器(只计算秒数)?
2. 应用程序进入后台(中键、搜索键、来电等)如何停止计数器?
3. WP7正在关闭我的申请流程时,我该怎么办?例如将当前游戏保存到独立存储中。
【问题讨论】:
我正在用 c#silverlight 编写扫雷游戏。
1. 我怎样才能在这个应用程序中添加一个计数器(只计算秒数)?
2. 应用程序进入后台(中键、搜索键、来电等)如何停止计数器?
3. WP7正在关闭我的申请流程时,我该怎么办?例如将当前游戏保存到独立存储中。
【问题讨论】:
1) 你需要使用Timer
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
【讨论】: