我正在用c#silverlight写扫雷游戏 1.如何在此应用程序中添加计数器(仅计数秒数)? 2.当应用程序进入后台时(中间按钮,搜索按钮,来电等),如何停止计数器? 3.当WP7关闭我的申请流程时,我该怎么办?例如,将当前游戏保存到隔离存储。
答案 0 :(得分:1)
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