使用隔离存储在WP7中保存集合

本文关键字:保存 集合 WP7 隔离 存储 | 更新日期: 2023-09-27 18:24:40

我正在构建一个有多个锦标赛的应用程序,每个锦标赛都有多个收藏。

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

当应用程序关闭并打开时,它会被调用

非常感谢您的帮助!!:)


这是锦标赛主舱

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


    }
}

使用隔离存储在WP7中保存集合

Everything Serialized需要作为显式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