Silverlight:IsolatedStorageSettings在页面刷新之间保持数据

时间:2011-06-12 23:34:58

标签: c# silverlight

我正在使用IsolatedStorageSettings类来存储一些应用程序数据,这些数据应该在我的Silverlight导航应用程序页面刷新后保留。 数据存储在页面FirstPage.xaml中,并在SecondPage.xaml中检索。 如果我不刷新,以下代码可以正常工作。但是,如果我在SecondPage.xaml(第二页)上刷新,则值将从AppStore返回空。可能是什么原因。

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;
     }   
}

1 个答案:

答案 0 :(得分:5)

您没有将修改保存在IsolatedStorageSettings文件中, 你应该用这个

IsolatedStorageSettings.ApplicationSettings.Save();

请注意,您可以使用IsolatedStorageSettings.ApplicationSettings而不是IsolatedStorageSettings的新实例。 也不要保存对设置的每次修改,只需在App.Exit()事件处理程序中调用此方法,将数据保存到硬盘是非常耗时的。