I'm not able to save to the isolated storage?

后端 未结 2 1169
遇见更好的自我
遇见更好的自我 2021-01-28 11:38

I\'m trying to save my model in isolated storage:

var settings = IsolatedStorageSettings.ApplicationSettings;

CurrentPlaceNowModel model = new CurrentPlaceNowMo         


        
2条回答
  •  滥情空心
    2021-01-28 12:35

    I'd do this differently and make a specific check to see if the key exists.

    CurrentPlaceNowModel model; 
    
    using (var settings = IsolatedStorageSettings.ApplicationSettings)
    {
        if (settings.Contains("MODEL"))
        {
            model = settings["MODEL"] as CurrentPlaceNowModel;
        }
        else
        {
            model = new CurrentPlaceNowModel();
            settings.Add("MODEL", model);    
            settings.Save();
        }
    }
    

    This pattern of working with IsolatedStorage definitely works.

    The only reason that this wouldn't work would be if CurrentPlaceNowModel could not be serialized with the DataContractSerializer. This is what the ApplicationSettings uses internally to serialize objects.
    You can test this by serialising it this way yourself to see what happens.

提交回复
热议问题