2010-08-30 9 views
0

我在wp7模擬器中遇到了奇怪的現象。Windows Phone 7:模擬器上的應用程序開啓/關閉事件難度很大

我有一個死簡單的應用程序這主要是直接從2010年

VS產生從的App.xaml模板:從App.xaml.cs

<!--Required object that handles lifetime events for the application--> 
    <shell:PhoneApplicationService 
     Launching="Application_Launching" Closing="Application_Closing" 
     Activated="Application_Activated" Deactivated="Application_Deactivated"/> 

墓碑代碼:

private void LoadSettings() 
    { 
     IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings; 

     ICollection<TicTacSpot> getSpots; 
     if (settings.TryGetValue<ICollection<TicTacSpot>>("spots", out getSpots)) 
     { 
      Spots = getSpots; 
     } 

     if (Spots == null) 
     { 
      Spots = new List<TicTacSpot>(); 
     } 
    } 

    private void SaveSettings() 
    { 
     IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings; 
     settings["spots"] = Spots; 
    } 

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

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

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

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

看起來很簡單。我在所有這些方法中設置了斷點。當我按F5部署的應用,被打的事件處理程序是:

Application_Launching() Application_Deactivated()

奇怪的是,這些被擊中,即使仿真器不顯示應用程序的打開或關閉。

在模擬器中,我打開應用程序,玩耍,關閉它,然後重新打開它。我使用「後退」和「開始」按鈕關閉它。儘管如此,我無法讓任何事件處理程序再次受到打擊。

我在這裏做錯了什麼?

回答

2

調試會話是否仍然有效?

我發現如果您設置了在啓動時遇到的斷點而您沒有繼續一定的時間(例如< 10秒),則調試會話將斷開連接。操作系統會終止該應用程序。

2

像Dennis說的那樣,要調試Activated事件處理程序,您需要在按下後退按鈕後啓動一個新的調試會話。順序應該是:

  • 啓動調試
  • 與應用程序播放,點擊開始
  • 退出應用程序,調試會話停止
  • 打後退按鈕 - 黑色的屏幕上的模擬器
  • 啓動調試會話,快10秒後,OS終止應用程序
+0

謝謝,但這段代碼仍然無法正常工作。 (我用9個元素保存了一個集合,然後用0取回一個集合。)你知道這是爲什麼嗎?或者我應該提出一個新問題? – 2010-08-31 21:17:31

+0

我們可以在問題中看到處理程序的一些代碼(desactived/actived)嗎? – MatthieuGD 2010-09-02 12:42:32

+0

仿真器在關閉時似乎重置了IsolatedStorage系統:http://www.codebadger.com/blog/post/2010/09/03/Using-Isolated-Storage-on-Windows-Phone-7.aspx(文件末尾)。您是否嘗試將您的狀態保存到PhoneApplicationService.Current.State中? – MatthieuGD 2010-09-03 21:58:28

相關問題