2014-03-26 63 views
1

我试图在IsolatedStorage中存储Reminder。它在运行时工作,但如果我重新启动应用程序,所有数据都消失了。存储提醒IsolatedStorageSettings.ApplicationSettings

这里是一些代码取笑一下:

private IsolatedStorageSettings userSettings = IsolatedStorageSettings.ApplicationSettings; 

private List<ScheduledAction> getStorage() 
{ 
    if (!userSettings.Contains("notifications")) 
    { 
     userSettings.Add("notifications", new List<ScheduledAction>()); 
    } 

    return (List<ScheduledAction>)userSettings["notifications"]; 
} 
private void saveStorage(List<ScheduledAction> list) 
{ 
    userSettings["notifications"] = list; 
} 

private void test() 
{ 
    List<ScheduledAction> list = getStorage(); 
    Reminder alarm = new Reminder("name"); 
    list.Add(alarm); 
    saveStorage(list); 
} 

我现在的猜测,为什么不存储对象是Reminder不序列化。既然这不是我的对象,我能做些什么呢?

回答

1

每当我们在IsolatedStorageSettings.Save Method中添加或更新时,都需要在应用程序退出前保存。我对你的代码做了一些小改动,可能这会对你有所帮助。

private List<ScheduledAction> getStorage() 
{ 
    if (!userSettings.Contains("notifications")) 
    { 
     userSettings.Add("notifications", new List<ScheduledAction>()); 
     userSettings.Save(); 
    } 

    return (List<ScheduledAction>)userSettings["notifications"]; 
} 

private void saveStorage(List<ScheduledAction> list) 
{ 
    userSettings["notifications"] = list; 
    userSettings.Save(); 
} 
+0

啊,是的,我创建了'saveStorage'做'保存()'这里忘了:)现在我得到'System.Runtime.Serialization.InvalidDataContractException'。我想提醒是不可序列化的。 – PiTheNumber