I'm using code like this to save state of my view model in my phone app:
var dataStore =IsolatedStorageSettings.ApplicationSettings;if(dataStore.Contains(ModelKey))
dataStore[ModelKey]=Model;else
dataStore.Add(ModelKey,Model);
dataStore.Save();
And this to load it back:
var dataStore =IsolatedStorageSettings.ApplicationSettings;if(dataStore.Contains(ModelKey)){var lastFeed = dataStore[ModelKey]asMainViewModel;if(lastFeed !=null)Model= lastFeed;}
Unfortunately, when it's a simple class with a few properties (a string and a
datetime), it comes back as a virgin object. If I store a datetime directly,
with the very same code, it works fine. What does it take to get these app
settings to save correctly beyond simple types?
Place the [DataContract] attribute on the class that contains the data being saved, then place [DataMember] on each property that needs to be serialized. If you leave this attribute off of the property then it won't be saved. This is the reverse of XmlSerialization
where properties are saved unless you mark them to be ignored.
Joel Johnson
Windows Phone Developer MVP
@j2inet http://www.j2i.net
This seems to work OK when I bump out to the photo chooser, but when I leave the app via the start button and come back, there's no data there. I'm saving on both exiting events... but perhaps I'm doing something else wrong in my init code. Will get back
to it...
Make sure that in your App.xaml.cs class you put the code to save the temp files in the Deactivated event handler and the code to restore the temp files in the Activated event handler.
Closing the app will call the Closing event while tombstoning the app will call the Deactivated event. Tombstoning is when another window comes in front of the application. Keep in mind that your application may never be restored. If the user hits the Start
button and then goes to the apps page and restarts your application, the Launching event. When you hit the start button, the application will be tombstoned. To restore the application, you must press the back button. You must then push the debug button in
Visual Studio to restore it. This will call the Activated event.
Also, keep in mind that the phone can only store a certain amount of windows before the windows on the bottom will drop off the stack. Say you have your app running and then the user tombstones it. If the user keeps opening many windows while the app is
tombstoned, the phone may run out of room and your app will be dropped off the bottom of the list. To solve this, maybe ask the user if they want to restore the previous state when the application is launched and there are temporary files that you saved.
Jeff
Member
114 Points
21 Posts
Simple types won't save to IsolatedStorageSettings on phone
Sep 30, 2010 06:32 PM | LINK
I'm using code like this to save state of my view model in my phone app:
And this to load it back:
Unfortunately, when it's a simple class with a few properties (a string and a datetime), it comes back as a virgin object. If I store a datetime directly, with the very same code, it works fine. What does it take to get these app settings to save correctly beyond simple types?
wp7 isolated storage
POP Forums for MVC
JeffPutz.com
j2inet
Member
143 Points
61 Posts
Where's your data contract?
Sep 30, 2010 07:09 PM | LINK
Did you define a data contract for your complex type? If not then there are no properties marked to be saved.
Windows Phone Developer MVP
@j2inet http://www.j2i.net
Jeff
Member
114 Points
21 Posts
Re: Where's your data contract?
Sep 30, 2010 07:42 PM | LINK
I did not. Where does the documentation show that you have to do that?
POP Forums for MVC
JeffPutz.com
j2inet
Member
143 Points
61 Posts
Add the [DataContract] and [DataMember] attributes.
Oct 01, 2010 10:56 AM | LINK
Place the [DataContract] attribute on the class that contains the data being saved, then place [DataMember] on each property that needs to be serialized. If you leave this attribute off of the property then it won't be saved. This is the reverse of XmlSerialization where properties are saved unless you mark them to be ignored.
Windows Phone Developer MVP
@j2inet http://www.j2i.net
Jeff
Member
114 Points
21 Posts
Re: Add the [DataContract] and [DataMember] attributes.
Oct 04, 2010 10:21 PM | LINK
This seems to work OK when I bump out to the photo chooser, but when I leave the app via the start button and come back, there's no data there. I'm saving on both exiting events... but perhaps I'm doing something else wrong in my init code. Will get back to it...
POP Forums for MVC
JeffPutz.com
The Situation
Member
20 Points
15 Posts
Re: Add the [DataContract] and [DataMember] attributes.
Oct 05, 2010 01:58 AM | LINK
Make sure that in your App.xaml.cs class you put the code to save the temp files in the Deactivated event handler and the code to restore the temp files in the Activated event handler.
Closing the app will call the Closing event while tombstoning the app will call the Deactivated event. Tombstoning is when another window comes in front of the application. Keep in mind that your application may never be restored. If the user hits the Start button and then goes to the apps page and restarts your application, the Launching event. When you hit the start button, the application will be tombstoned. To restore the application, you must press the back button. You must then push the debug button in Visual Studio to restore it. This will call the Activated event.
Also, keep in mind that the phone can only store a certain amount of windows before the windows on the bottom will drop off the stack. Say you have your app running and then the user tombstones it. If the user keeps opening many windows while the app is tombstoned, the phone may run out of room and your app will be dropped off the bottom of the list. To solve this, maybe ask the user if they want to restore the previous state when the application is launched and there are temporary files that you saved.