2012-02-15 74 views
1

我正在构建一个包含多个比赛的应用程序,并且每个比赛都有多个集合。使用隔离存储在WP7中保存集合

只有我写锦标赛名称时,如果我尝试向它的集合添加任何内容,它才会崩溃,IsolatedStorage才能正常工作。

我的比赛类

public class TournamentMain 
{ 
    public int ID = 0; 
    public string name { get; set; } 
    public double buy_in { get; set; } 
    public double re_buy { get; set; } 
    public double add_on { get; set; } 
    public int blindindex = 1; 
    public int placeindex = 1; 
    public int playerindex = 1; 
    public ObservableCollection<Blind> blinds { get; set; } 
    public ObservableCollection<Player> players { get; set; } 
    public ObservableCollection<Place> places { get; set; } 
    public ObservableCollection<Paidplace> paidplaces {get; set;} 

    public TournamentMain() { 
     players = new ObservableCollection<Player>(); 
     places = new ObservableCollection<Place>(); 
     blinds = new ObservableCollection<Blind>(); 
     paidplaces = new ObservableCollection<Paidplace>(); 

    } } 

我的存储类

public class StorageSaveing 
{ 
    static XmlSerializer serializer; 
    public static void saveIT() 
    { 


     using (var store = IsolatedStorageFile.GetUserStoreForApplication()) 

      using (var stream = new IsolatedStorageFileStream("data.txt", 
                FileMode.Create, 
                 FileAccess.Write, 
                 store)) 
      { 

       serializer = new XmlSerializer(typeof(ObservableCollection<TournamentMain>)); 
       serializer.Serialize(stream, App.tournaments); 
      } 

    } 


    public static ObservableCollection<TournamentMain> loadIT() 
    { 
    using( var store = IsolatedStorageFile.GetUserStoreForApplication()) 
    using (var stream = new IsolatedStorageFileStream("data.txt", 
              FileMode.OpenOrCreate, 
               FileAccess.Read, 
               store)) 

     using( var reader = new StreamReader(stream)) 
     { 
     serializer = new XmlSerializer(typeof(ObservableCollection<TournamentMain>)); 
     return reader.EndOfStream 
       ? new ObservableCollection<TournamentMain>() 
      : (ObservableCollection<TournamentMain>)serializer.Deserialize(reader); 
     } 
    } 
} 

它叫出来的时候应用程序关闭和打开

任何帮助,非常感谢!! :)


这里是TournamentMain类

using System; 
using System.Net; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Documents; 
using System.Windows.Ink; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Animation; 
using System.Windows.Shapes; 
using System.Collections.ObjectModel; 
using System.Collections.Generic; 

namespace PokerAssistant 
{ 

    public class TournamentMain 
    { 
     public int ID = 0; 
     public string name { get; set; } 
     public double buy_in { get; set; } 
     public double re_buy { get; set; } 
     public double add_on { get; set; } 
     public int blindindex = 1; 
     public int placeindex = 1; 
     public int playerindex = 1; 
     private ObservableCollection<Blind> _blinds; 
     public ObservableCollection<Blind> blinds { get{ 
      return _blinds; 
     } 
      set { 
       _blinds = value; 
      } 
     } 

     public ObservableCollection<Player> players { get; set; } 
     public ObservableCollection<Place> places { get; set; } 
     public ObservableCollection<Paidplace> paidplaces {get; set;} 

     public TournamentMain() { 
      players = new ObservableCollection<Player>(); 
      places = new ObservableCollection<Place>(); 
      _blinds = new ObservableCollection<Blind>(); 
      paidplaces = new ObservableCollection<Paidplace>(); 

     } 

     public double calculatePot() { 
      double totalsum = 0; 
      foreach (Player player in players) 
      { 
       totalsum += player.cash; 
      } 
      foreach (Blind blind in blinds) 
      { 
       totalsum += blind.Ante * players.Count; 
      } 
      return totalsum; 
     } 

     public void setPlacesList() { 

      int i=1; 
      foreach(Double place in calculatePlaces()){ 
       Paidplace p = new Paidplace(); 
       p.name = i + ". " + place + "$"; 
       paidplaces.Add(p); 
       i++; 

      } 

     } 

     public List<double> calculatePlaces() { 
      List<double> paidplaces = new List<double>(); 
      double total = 0; 
      foreach (Place place in places) 
      { 
       paidplaces.Add(calculatePot() * (place.place_pr/100)); 
       total += calculatePot() * (place.place_pr/100); 
      } 
      total = calculatePot()-total; 
      paidplaces.Add(total); 

      return paidplaces; 
     } 

     public int playersCount() { 
      return players.Count; 
     } 





    } 
} 

回答

3

一切序列化需要显式getter和setter

public class TournamentMain 
{ 
    public int ID { get; set; } 
    public string name { get; set; } 
    public double buy_in { get; set; } 
    public double re_buy { get; set; } 
    public double add_on { get; set; } 
    public int blindindex { get; set; } 
    public int placeindex { get; set; } 
    public int playerindex { get; set; } 
    public ObservableCollection<Blind> blinds { get; set; } 
    public ObservableCollection<Player> players { get; set; } 
    public ObservableCollection<Place> places { get; set; } 
    public ObservableCollection<Paidplace> paidplaces {get; set;} 

    public TournamentMain() 
    { 
     ID = 0; 
     blindindex = 1; 
     placeindex = 1; 
     playerindex = 1; 
     players = new ObservableCollection<Player>(); 
     places = new ObservableCollection<Place>(); 
     blinds = new ObservableCollection<Blind>(); 
     paidplaces = new ObservableCollection<Paidplace>(); 
    } 

}

还要确保对象播放机,广场,失明,Paidplace有explicite getter和setter

+0

增加了一个像这样的集合,但仍然崩溃:S 'private ObservableCollection _blinds; public ObservableCollection blinds {get { return _blinds; } set { _blinds = value; } }' – Mikk 2012-02-16 12:56:01

+0

你可以发布整个TournamentMain类 – MyKuLLSKI 2012-02-16 14:32:01

+0

发布的代码在 – Mikk 2012-02-22 13:45:17