2015-10-23 138 views
0

我在我的項目中有兩個頁面。第一頁是關於創建MyPoint對象,然後將它添加到列表,並最終將其保存到獨立存儲如下:獨立存儲 - Windows Phone 8

IsolatedStorageSettings appSettings = IsolatedStorageSettings.ApplicationSettings; 

點類:

public class MyPoint 
    { 

     public Ellipse Point { get; set; } 
     public string Tag { get; set; } 
     public GeoCoordinate Coords { get; set; } 
     public string City { get; set; } 
     public string PostalCode { get; set; } 

     public MyPoint() { } 

     public MyPoint(Ellipse point, GeoCoordinate cords, string tag, string city, string postalCode) 
     { 
      this.Point = point; 
      this.Coords = cords; 
      this.Tag = tag; 
      this.City = city; 
      this.PostalCode = postalCode; 
     } 
    } 

保存到獨立存儲方法:

private void SaveAppSettings() 
     { 
      try 
      { 
       appSettings.Remove("points"); 
      } 
      catch { } 
      try 
      { 
       appSettings.Add("points", this.points); 
       appSettings.Save(); // this throws an Exception 
      } 
      catch { }; 
     } 

Secound`s頁面加載方法

try 
     { 
      List<MyPoint> points = (List<MyPoint>)appSettings["points"]; 
      foreach (MyPoint p in points) 
      { 
       source.Add(new PointsBook(p.Tag, p.Coords, p.City, p.PostalCode)); 
      } 
     } 

關鍵是

appSettings.Save()thorws是一個例外,即使我評論此行全部工作。我已經保存了分數,我可以通過這兩頁來閱讀它們。

問題是當我關閉應用程序時,我的appSetting有一個字符串「points」,但裏面沒有任何東西。它只有[「鍵」],但沒有[值]。

+0

什麼是appSettings?添加一切在問題 –

+1

請參閱:http://stackoverflow.com/a/13571938/869621 –

+0

無論如何,我從MyPoint屬性中刪除GeoCorrdinate - 仍然無法正常工作 – miechooy

回答

0

問題解決了!

MyPoint對象在構造函數中有Ellipse。 IsolatedStorage拋出關於UIElement的異常無法序列化。

現在一切工作正常!

+0

MyPoint看起來像它屬於應用程序的「模型」部分,而橢圓屬於「視圖」,將它們放在一起不是一個好主意 – Corcus

相關問題