2011-09-12 44 views
1

我需要在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; 
} 

我不想手動序列化的每個對象的增加,我想打自定義類可以默認序列化以將其存儲在獨立存儲中(如果可能的話)

回答

2

我的初始猜測是序列化問題。你所有的房產都有公共籌款人嗎?發佈您正在存儲的課程以及您用於存儲它們的代碼。

我相信IsolatedStorageSettings默認使用DataContractSerializer。

DataContractSerializer Class

你可能會嚴格創建一個單獨的對象存儲的目的:如果你想ExtentHistory被序列化,你應該在你需要做的就是它與串行器正常工作,什麼讀了隔離存儲器中的數據(有點像DTO)。這將允許您保持ExtentHistory原樣。

+0

請看看我編輯的文章 –

+0

更新了我的答案。 –

+0

感謝您的回覆。這是解決方案。 –

相關問題