2

出于某种原因,每次我重新启动(在浏览器中)silverlight 应用程序时,隔离存储中都没有密钥。

我什至用下面的代码用香草模板试过这个。我检查过的东西:

  1. 总是使用同一个端口

  2. 启动时,应用程序始终在隔离存储中创建条目(从不持久)

  3. 关机时,密钥始终存在于隔离存储中

代码:-

 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;
    }
}

欢迎任何帮助。

4

1 回答 1

2

为了将对象序列化到应用程序设置中,每种涉及的类型(在您的情况下ParentChild)都必须具有公共默认构造函数,并且需要序列化的属性必须具有公共 getter 和 setter 过程。

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.

于 2010-08-12T16:50:24.133 回答