开发者

silverlight isolated storage - not persisting across application restarts

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 MainP开发者_运维知识库age()
        {
            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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜