I have a Windows 8 application. This application has several custom-defined classes. I need to store instances of these instances into Isolated Storage. From my understanding, Isolated Storage has been replaced with ApplicationDataContainer. Currently, I’m trying the following:
public class MyClass
{
private HttpClient service = new HttpClient();
public string FirstName { get; set; }
public DateTime? BirthDate { get; set; }
public int Gender { get; set; }
public async Task Save()
{
// Do stuff...
}
}
...
MyClass myInstance = new MyInstance();
// do stuff...
try {
ApplicationDataContainer storage = ApplicationData.Current.LocalSettings;
if (storage != null)
{
if (storage.Values.ContainsKey("MyKey"))
storage.Values["MyKey"] = myInstance;
else
storage.Values.Add("MyKey", myInstance);
}
} catch (Exception ex)
{
MessageDialog dialog = new MessageDialog("Unable to save to isolated storage");
dialog.ShowAsync();
}
What am I missing. Why is an exception always being thrown. The exception is not very descriptive. Its just a generic System.Exception and the message doesn’t help either. Can someone please help me?
Thank you
The exception I get from the code above seems pretty clear:
Per Accessing app data with the Windows Runtime
You can use the ApplicationDataCompositeValue class to group settings that must be treated atomically (but they still need to be supported runtime data types). Scenario 4 of the Application Data Sample covers this.
In your specific case though, you may want to consider serializing to a file and using app file storage versus settings.