Persist Attribute and problem with PageFrame Binding (WP7)

Apr 20, 2011 at 7:38 AM
Edited Apr 20, 2011 at 9:39 AM

Backgroud

I wanted an easy way to persist data in my view model to isolated storage such as a settings page. The SurviveTombstone Attribute did 80%  of what i wanted but the problem is that the data is lost if the application is closed and launched again, this can be done by pressing windows button, going to applications and launching the application again. The reason for this is that SurviveTombstone only stores data in the PhoneService.State dictionary which is lost when you application is terminated. 

Solution

My solution for this problem was to copy the Tombstone Attributes and modify it to save to IsolatedStorageSettings. The syntax to persist a property looks like this

 

    [Persist]
    public class SettingsViewModel : PropertyChangedBase
    {
        private string _name;
        [Persist]
        public string Name
        {
            get { return _name; }
            set
            {
                _name = value;
                NotifyOfPropertyChange(() => Name);
            }
        }

    }

 

I got this working with one minor problem, to retrive the data from isolated storage the RootFrame.Context in the PhoneBootStrapper must be set and the bindings need to be set up. The problem i found was when loading the Main page when the OnNavigated event handler is hit in the PhoneBootStrapper the RootFrame has been set but the bindings have not been applied. I got around this by exposing the SelectInstancesToResurrect method and my new method RetrieveFromIsolatedStorage and calling them from the MainPage.Loaded event handler like this.

 

    public partial class SettingsView
    {

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            var bootStrapper = Application.Current.Resources["bootstrapper"] as PhoneBootstrapper;
            bootStrapper.RetrieveFromIsolatedStorage(bootStrapper.SelectInstancesToResurrect());
        }
    }

 

Problem

So where would be the best place to call my RetrieveFromIsolatedStorage after the bindings have been applied.

 

For reference i uploaded a sample project here;

Sample

http://cid-45301901ee7e4a69.office.live.com/self.aspx/Caliburn/PersistenceSample.zip

Modified Classes

PhoneBootstrapper

IPersistable (new)

Persist (New)