2010-08-12 58 views
2

由于某种原因,每次我重新启动(在浏览器中)Silverlight应用程序时,隔离存储都没有密钥。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; 
    } 
} 

任何帮助将受到欢迎。

回答

2

为了连载对象为应用程序设置每个涉及的类型(在你的情况下,两个ParentChild)必须有一个公共的默认构造函数和需要连载必须具有公共getter和setter方法的属性。

通过使用System.Runtime.Serialization命名空间中的一些属性(如DataMember),可以获得一些额外的控制权。

此外,您还没有拨打IsolatedStorageSettings.ApplicationSettings.Save,所以无论如何都不会在商店中产生任何结果。

相关问题