[Silverlight] 當麻許的Phone7筆記 - 22.關於墓碑機制..(Tombstoning)

2011/04/01

很久沒繼續寫下去了..
今天討論一個比較無聊但是卻是超級重要的東西…
墓碑機制…

如果手機程式跑到一半,電話來了,或是,不小心壓到搜尋鍵(很常發生)..
回來程式後..該怎麼繼續執行下去,如何儲存狀態就是在這裡…
所以配合 IsolatedStorageFile 就無敵拉…

當建立好專案的之後…
開啟 App.xaml.cs

blog-194


之後你會看到下列這一段程式碼…其中我加入中文…

// 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)
{
 
}
 
// 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)
{
 
}
 
// 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)
{
}
 
// 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)
{
 
}


很好有點抽象…
我們加入一些MessageBox 的跳出事件來觀察,他到底是何時被呼叫…

加入Code 如下:
// 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)
{
MessageBox.Show("啟動 : Application_Launching");
}
 
// 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)
{
MessageBox.Show("被重新啟動繼續 : Application_Activated");
}
 
// 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)
{
MessageBox.Show("被中斷 : Application_Deactivated");
}
 
// 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)
{
MessageBox.Show("關閉中 : Application_Closing");
}


所以程式依執行起來之後如期顯示: 啟動 : Application_Launching
blog-195

我們點擊退後鍵則呼叫了 關閉中 : Application_Closing

blog-196

這時候我們再回來程式他是呼叫  : 啟動 : Application_Launching

blog-197

這時候我們按下搜尋鍵 他會呼叫  被中斷 : Application_Deactivated
blog-198

之後我們按下到退鍵回到Application 則會呼叫 被重新啟動繼續 : Application_Activated

blog-199

這樣大概可以知道一下 這四個事件是何時被呼叫的..

不過為啥要叫做墓碑機制…

始終,是一個謎…


讚一下:


0 意見:

程式 . 生活 . D小調.@2010 | Binary Design: One Winged Angel.