views:

88

answers:

2

for some reason everytime i restart the (in browser) silverlight application, isolated storage has no keys in it.

i have even tried this with a vanilla template with the code below. things i have checked:

  1. always using the same port

  2. on startup the application always created the entry in isolated storage (never persisted)

  3. on shutdown, the keys are always present in isolated storage

Code:-

 namespace SilverlightApplication1  
 {

    public partial class MainPage : UserControl, INotifyPropertyChanged
    {

        private string _ChildCount;
        public string ChildCount
        {
            get { return _ChildCount; }
            set { NotifyPropertyChangedHelper.SetProperty(this, ref _ChildCount, value, "ChildCount", PropertyChanged); }
        }


        public MainPage()
        {
            InitializeComponent();
            SaveData();

        }

        ~MainPage()
        {
            CheckData();
        }

        private void SaveData()
        {
            if (!IsolatedStorageSettings.ApplicationSettings.Contains("childCheck"))
            {
                IsolatedStorageSettings.ApplicationSettings.Add("childCheck", Parent.Create(5, 5));
                ChildCount = "Created Children(5)";
            }
            else
                CheckData();
        }

        private void CheckData()
        {
            if (IsolatedStorageSettings.ApplicationSettings.Contains("childCheck"))
            {
                if (((Parent)IsolatedStorageSettings.ApplicationSettings["childCheck"]).Children.Length == 5)
                    ChildCount = "Children Present";
                else
                    ChildCount = "Parent present without children";
            }
            else
                ChildCount = "Children not found";
        }



        public class Parent
        {
            public int Id { get; private set; }
            public Child[] Children { get; private set; }
            private Parent() { }

            public static Parent Create(int id, int childCount)
            {
                var result = new Parent
                {
                    Id = id,
                    Children = new Child[childCount]
                };

                for (int i = 0; i < result.Children.Length; i++)
                    result.Children[i] = Child.Create(i);

                return result;
            }

        }

        public class Child
        {
            public int Id { get; private set; }
            private Child() { }

            public static Child Create(int id)
            {
                return new Child { Id = id };
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }
}

any help would be welcome.

+1  A: 

In order to serialise an object into application settings each type involved (in your case both Parent and Child) must have a public default constructor and the properties that need serialising must have public getter and setter procedures.

You can gain a little extra control by using some of the attributes in the System.Runtime.Serialization namespace such as DataMember.

In addition you are haven't called IsolatedStorageSettings.ApplicationSettings.Save so nothing would end up in the store anyway.

AnthonyWJones
A: 

ApplicationSettings don't persist beyond Application End. You can use ApplicationSettings to transfer or get data across the application and it supports data transfer across browsers too (for eg. From Chrome to Firefox etc..).

Try saving data as XElement in a file in the IsolatedStorage and try retieving it during Application start.

To save the data, try this

var doc = new XDocument(
            new XElement(// The elements that you want to insert
            )
           );

using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
            {                    
                IsolatedStorageFileStream isfs = new IsolatedStorageFileStream("Data.xml", FileMode.Append, isf);
                {
                    doc.Save(isfs);
                }
            }

To load the file, try this.

using (var isf = IsolatedStorageFile.GetUserStoreForApplication())
        {
            if (isf.FileExists("Data.xml"))
            {
                try
                {
                    using (var isfStream = new IsolatedStorageFileStream("Data.xml", FileMode.Open, isf))
                    {
                        Debug.WriteLine("Writing");

                        var Xelem = XElement.Load(isfStream);

                        foreach (var n in Xelem.Elements())
                        {
                            string str = n.Value;

                           // Do what you need to do
                        }
                    }
                }
               catch (IsolatedStorageException ex)
                {
                   // Catch how you want to
                }
          }
      }

Hope this helps.

Aswin Ramakrishnan