我正在尝试将此类序列化和反序列化为 Windows Phone ApplicationSettings,但由于某种原因,仅保留Items
了 or 属性,而不是Title
orLastChanged
属性。任何关于为什么的想法都会受到赞赏!
班级:
public class VehicleCollection : ObservableCollection<Vehicle>
{
public string Title { get; set; }
public DateTime LastChanged { get; set; }
public bool HasNoItems { get { return Items.Count == 0; } }
public VehicleCollection() { }
public VehicleCollection(string title, DateTime lastChanged)
{
Title = title;
LastChanged = lastChanged;
}
protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
{
base.OnCollectionChanged(e);
OnPropertyChanged(new PropertyChangedEventArgs("HasNoItems"));
}
}
坚持逻辑:
public static bool SavePersistent(string key, object value)
{
if (null == value)
return false;
var store = IsolatedStorageSettings.ApplicationSettings;
if (store.Contains(key))
store[key] = value;
else
store.Add(key, value);
store.Save();
return true;
}
public static T LoadPersistent<T>(string key)
{
var store = IsolatedStorageSettings.ApplicationSettings;
if (!store.Contains(key))
return default(T);
return (T)store[key];
}
编辑:我创建了一个行为明显的示例项目。http://www.fileswap.com/dl/2ar0ygF8w7/
- 运行应用程序并观察正在创建的静态数据
- 按“保存数据”保存到独立存储
- 关闭并重新运行应用程序
- 观看两个集合的标题消失,因为它们没有持久化