我需要在IsolatedStorage中存儲不同的對象,並使用IsolatedStorageSettings類來完成此操作。其中一些對象是基本類型,因此可以很好地存儲和檢索。但其中一些是自定義類的實例,它們存儲的很好,但是當我嘗試檢索它們時,我得到了具有初始值的實例。 如何將自定義類實例存儲在IsolatedStorage中並檢索它們?將自定義類實例存儲在Silverlight中的IsolatedStorage中
菲爾·桑德勒,我想是的。但我不知道什麼類型的序列化使用獨立存儲,所以我不知道如何讓我的類可序列化。私人領域也必須存儲。 這是自定義類的代碼:
public class ExtentHistory : INotifyPropertyChanged
{
private const int Capacity = 20;
private List<Envelope> _extents;
private int _currentPosition;
public event PropertyChangedEventHandler PropertyChanged;
public int ItemsCount
{
get { return _extents.Count; }
}
public bool CanStepBack
{
get { return _currentPosition > 0; }
}
public bool CanStepForward
{
get { return _currentPosition < _extents.Count - 1; }
}
public Envelope CurrentExtent
{
get { return (_extents.Count > 0) ? _extents[_currentPosition] : null; }
}
public ExtentHistory()
{
_extents = new List<Envelope>();
_currentPosition = -1;
}
public void Add(Envelope extent)
{
if (_extents.Count > Capacity)
{
_extents.RemoveAt(0);
_currentPosition--;
}
_currentPosition++;
while (_extents.Count > _currentPosition)
{
_extents.RemoveAt(_currentPosition);
}
_extents.Add(extent);
}
public void StepBack()
{
if (CanStepBack)
{
_currentPosition--;
NotifyPropertyChanged("CurrentExtent");
}
}
public void StepForward()
{
if (CanStepForward)
{
_currentPosition++;
NotifyPropertyChanged("CurrentExtent");
}
}
private void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
這裏是存儲和檢索的功能:
private IsolatedStorageSettings _storage;
public void Store(string key, object value)
{
if (!_storage.Contains(key))
{
_storage.Add(key, value);
}
else
{
_storage[key] = value;
}
}
public object Retrieve(string key)
{
return _storage.Contains(key) ? _storage[key] : null;
}
我不想手動序列化的每個對象的增加,我想打自定義類可以默認序列化以將其存儲在獨立存儲中(如果可能的話)
請看看我編輯的文章 –
更新了我的答案。 –
感謝您的回覆。這是解決方案。 –