使用isolatedstorage在WP7中保存集合

时间:2012-02-15 20:40:40

标签: c# windows-phone-7 isolatedstorage

我正在构建一个包含多个锦标赛的应用,每个锦标赛都有多个系列。

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;
        }





    }
}

1 个答案:

答案 0 :(得分: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>();
   } 

}

同时确保对象Player,Place,Blind和Paidplace具有明确的getter和setter