出于某种原因,每次重新启动(在浏览器中)silverlight应用程序时,隔离存储都没有密钥。
我甚至尝试使用带有以下代码的香草模板。 我检查过的事情:
始终使用相同的端口
启动时,应用程序始终在隔离存储中创建条目(永不保留)
关闭时,密钥始终存在于隔离存储中
代码: -
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;
}
}
欢迎任何帮助。
答案 0 :(得分:2)
为了将对象序列化为应用程序设置,所涉及的每种类型(在您的情况下都是Parent
和Child
)必须具有公共默认构造函数,并且需要序列化的属性必须具有公共getter和setter程序
您可以使用System.Runtime.Serialization
命名空间中的某些属性(例如DataMember
)获得一些额外的控制权。
此外,你还没有打电话给IsolatedStorageSettings.ApplicationSettings.Save
,所以无论如何都不会在商店里结束。