2011-06-12 91 views
2

我使用IsolatedStorageSettings類來存儲一些應用程序數據,這些數據在我的Silverlight導航應用程序的頁面刷新後應該保留。 數據存儲在頁面FirstPage.xaml中,並在SecondPage.xaml中檢索。 下面的代碼工作得很好,如果我不刷新。但是,如果我在SecondPage.xaml(第二頁)上進行刷新,則值將從AppStore返回空。可能是什麼原因。Silverlight:IsolatedStorageSettings在頁面刷新之間保留數據

public static class AppStore 
{ 
    private static IsolatedStorageSettings appSettings = IsolatedStorageSettings.ApplicationSettings;  

    public static String MyData 
    { 
     get 
     {     
      if (appSettings.Contains("MyData")) 
      { 
       return(appSettings["MyData"].ToString());     
      }     
      return String.Empty; 
     } 
     set 
     { 
      if (!appSettings.Contains("MyData")) 
      { 
       appSettings.Add("MyData", string.Empty); 
      } 
      appSettings["MyData"] = value;     
     } 
    } 
} 

public partial class FirstPage : Page 
{ 
    private string data = "somevalue"; 
    . 
    . 
    public FirstPage() 
    { 
     AppStore.MyData = data; 
    } 
} 


public partial class SecondPage : Page 
{  
    public SecondPage() 
    { 
     ContentText.Text = AppStore.MyData; 
    } 
} 

回答

5

你不保存在IsolatedStorageSettings文件的修改, 你應該使用這個

IsolatedStorageSettings.ApplicationSettings.Save(); 

注意,您可以使用IsolatedStorageSettings.ApplicationSettings代替IsolatedStorageSettings的一個新的實例。 也不保存每個修改到您的設置,只需在您的App.Exit()事件處理程序中調用此方法,將數據保存到硬盤是費時的。

+0

實際上,我不需要在不同的應用程序運行之間保存數據,只需要在頁面刷新之間進行維護。什麼是最好的方式來做到這一點? – devnull 2011-06-13 02:30:23

+0

刷新頁面時,應用程序將退出並重新加載,並且託管內存中保存的數據將丟失,您需要將其保存到硬盤中。 – Waleed 2011-06-13 02:33:28

相關問題