将文件保存到WP8.1

时间:2016-05-11 17:15:20

标签: c# xna savefiledialog

.Hi,好几天了,我正试图找到修复我的代码。我正在尝试保存并加载我游戏所需的所有数据,这在Windows版本的游戏中运行良好,但当我尝试将其移植到WP8.1时,我收到的错误如下:访问被拒绝(保存时,有时)和“与此oplock关联的句柄已关闭。现在,oplock已被破坏。”这是我的代码,我认为我没有以正确的方式关闭文件。

public class SaveLoadV2
{

    public PlayField field;
    public List<Sprite.Info> Innerspritelist = new List<Sprite.Info>();
    public List<Game1.Score> Scores;
    public List<Sprite.Info> Innerspritelist2 = new List<Sprite.Info>();
    public ToSave tosave = new ToSave();
    public SkyMan.ScoreInfo skyman;

    public struct ToSave 
    {   
        public List<Sprite.Info> Innerspritelist2;

        public List<Game1.Score> Scores2;

        public SkyMan.ScoreInfo skyman;


    }

    StorageFile sampleFile;

    async void CreateFile()
    {
        // Create sample file; replace if exists.
        StorageFolder storageFolder =
            ApplicationData.Current.LocalFolder;
        StorageFile sampleFile =
            await storageFolder.CreateFileAsync("Savegame.sav",
                CreationCollisionOption.ReplaceExisting);
    }

    async public void SaveLoadDOV2(PlayField field, string action, List<Game1.Score> score, SkyMan.ScoreInfo skyman)
    {
        this.skyman = skyman;
        this.Scores = score;
        this.field = field;
        if (action == "SAVE")
            await this.InitiateSave();
        else if (action == "LOAD")
            await this.InitiateLoad();
    }
    /// <summary>
    /// Save
    /// </summary>
    async Task InitiateSave()
    {
        tosave.Innerspritelist2 = new List<Sprite.Info>();

        foreach (Sprite sprite in field.onthefield)
        {
            tosave.Innerspritelist2.Add(sprite.Save());//add every Sprite to the list
        }
        tosave.Scores2 = Scores;

        tosave.skyman = skyman;

        //start the saving


        //CreateFile();

        //XmlSerializer serializer = new XmlSerializer(typeof(ToSave));
        DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(ToSave));
        var stream = await ApplicationData.Current.LocalFolder.OpenStreamForWriteAsync(
                          "savegame.sav",
                          CreationCollisionOption.ReplaceExisting);

        serializer.WriteObject(stream, tosave);








}

    /// <summary>
    /// Load
    /// </summary>
    /// <returns></returns>

    async Task   InitiateLoad()
    {
        //initiates the loading

        DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(ToSave));

        using (var myStream = await ApplicationData.Current.LocalFolder.OpenStreamForReadAsync("savegame.sav"))
        {
            tosave =  (ToSave)serializer.ReadObject(myStream);
        }



        //AfterLoad();


    }

    public  PlayField AfterLoad()//returns the field list 
    {
        int i = 0;
        foreach (Sprite sprite in field.onthefield)
        {
            try
            {
                sprite.Load(tosave.Innerspritelist2[i]);
                i++;
            }
            catch//if it dose not work means that there is not a save file and creates one
            {
                InitiateSave();
                break;
            }

        }
        return this.field;
    }

    public List<Game1.Score> ReturnScores()//for score sync 
    {
        return tosave.Scores2;
    }
    public SkyMan.ScoreInfo ReturnSkyMan()//for player score and live sync
    {
        return tosave.skyman;
    }


}

对于乱码,我不停地玩它。

0 个答案:

没有答案