2011-11-21 35 views
0

正如在此問題的標題中所述,我試圖在應用程序關閉時保留Timespan值。這就是情況......我正在編寫一個Windows小工具,每當彈出窗口關閉時就會銷燬它,以及Timespan值。我需要它,所以每次關閉彈出窗口時都會保留這個值,這將如何完成?在退出時保留Silverlight應用程序中的值

我現在正在做的代碼如下。

SilverlightGadgetUtilities.Stopwatch watch = new SilverlightGadgetUtilities.Stopwatch(); 

    private void Application_Startup(object sender, StartupEventArgs e) 
    { 
     watch.currentTime(); 
     this.RootVisual = new Page(); 

    } 

    private void Application_Exit(object sender, EventArgs e) 
    { 

     watch.currentTime(); 
    } 

這是我的秒錶類:

public TimeSpan? currentTime() 
    { 
     current = Elapsed; 
     return current; 
    } 

    public TimeSpan? Elapsed 
    { 
     get 
     { 
      return new TimeSpan(this.GetElapsedDateTimeTicks() * 10000000); 
     } 
    } 

GetElapsedDateTimeTicks()使用DateTime.Now.Second()的時機。再次

謝謝!

回答

2

您可以將數據存儲在應用程序的隔離存儲設置中,並在啓動時檢索它。

這裏是存儲IsolatedStorageSettings信息的一個例子:

IsolatedStorageSettings.ApplicationSettings.Add("MySettingName", MySetting); 

然後,您可以使用檢索:

IsolatedStorageSettings.ApplicationSettings["MySettingName"]; 

IsolatedStorageSettings.ApplicationSettings行爲很像一本字典。您應該檢查是否已經存儲了該名稱的設置,如果存在,請將其刪除或覆蓋它。覆蓋它可以做像這樣:

if (!IsolatedStorageSettings.ApplicationSettings.Contains("MySettingName")) 
    IsolatedStorageSettings.ApplicationSettings.Add("MySettingName", MySetting); 
else 
    IsolatedStorageSettings.ApplicationSettings["MySettingName"] = MySetting; 

的代碼刪除並重新添加有異曲同工之處,只是換了別的塊:與同

else 
{ 
    IsolatedStorageSettings.ApplicationSettings.Remove("MySettingName"); 
    IsolatedStorageSettings.ApplicationSettings.Add("MySettingName", MySetting); 
} 
+0

我得到一個錯誤「的項目鍵已經添加'我需要在使用前實例化一個新的? –

+0

*錯誤我的意思是異常。 –

+0

嘗試IsolatedStorageSettings.ApplicationsSettings [「MySettingName」] = MySetting;代替。在嘗試訪問它之前,請確保它存在。例如:if(IsolatedStorageSettings.ApplicationSettings.Contains(「MySettingName」)) {var setting = IsolatedStorageSettings.ApplicationSettings [「MySettingName」]; } –

相關問題