2

我在保存我的游戏时遇到了问题,我花了几个小时寻找解决方案,但没有任何运气。我使用了写在某人博客中的这段代码:

public class SaveandLoad
{
    StorageDevice device;
    string containerName = "ChainedWingsContainer";
    string filename = "mysave.sav";
    public struct SaveGame
    {
        public int s_mission;
    }

    public void InitiateSave()
    {
        if (!Guide.IsVisible)
        {
            device = null;
            StorageDevice.BeginShowSelector(PlayerIndex.One, this.SaveToDevice, null);
        }
    }

    void SaveToDevice(IAsyncResult result)
    {
        device = StorageDevice.EndShowSelector(result);
        if (device != null && device.IsConnected)
        {
            SaveGame SaveData = new SaveGame()
            {
                s_mission = Game1.mission,
            };
            IAsyncResult r = device.BeginOpenContainer(containerName, null, null);
            result.AsyncWaitHandle.WaitOne();
            StorageContainer container = device.EndOpenContainer(r);
            if (container.FileExists(filename))
                 container.DeleteFile(filename);
            Stream stream = container.CreateFile(filename);
            XmlSerializer serializer = new XmlSerializer(typeof(SaveGame));
            serializer.Serialize(stream, SaveData);
            stream.Close();
            container.Dispose();
            result.AsyncWaitHandle.Close();
        }
    }

    public void InitiateLoad()
    {
        if (!Guide.IsVisible)
        {
            device = null;
            StorageDevice.BeginShowSelector(PlayerIndex.One, this.LoadFromDevice, null);
        }
    }

    void LoadFromDevice(IAsyncResult result)
    {
        device = StorageDevice.EndShowSelector(result);
        IAsyncResult r = device.BeginOpenContainer(containerName, null, null);
        result.AsyncWaitHandle.WaitOne();
        StorageContainer container = device.EndOpenContainer(r);
        result.AsyncWaitHandle.Close();
        if (container.FileExists(filename))
        {
            Stream stream = container.OpenFile(filename, FileMode.Open);
            XmlSerializer serializer = new XmlSerializer(typeof(SaveGame));
            SaveGame SaveData = (SaveGame)serializer.Deserialize(stream);
            stream.Close();
            container.Dispose();
            //Update the game based on the save game file
            Game1.mission = SaveData.s_mission;
        }
    }
}

但是每当我运行它时,我都会收到以下消息:

Microsoft.Xna.Framework.Storage.dll 中出现“System.InvalidOperationException”类型的未处理异常

附加信息:在此 PlayerIndex 使用的所有先前容器都被释放之前,无法打开新容器。

我四处寻找答案,大多数建议都建议使用 Using 语句。所以我使用了这样的:

    void SaveToDevice(IAsyncResult result)
    {
        device = StorageDevice.EndShowSelector(result);
        if (device != null && device.IsConnected)
        {
            SaveGame SaveData = new SaveGame()
            {
                s_mission = Game1.mission,
            };
            IAsyncResult r = device.BeginOpenContainer(containerName, null, null);
            result.AsyncWaitHandle.WaitOne();
            using (StorageContainer container = device.EndOpenContainer(r))
            {
                if (container.FileExists(filename))
                    container.DeleteFile(filename);
                Stream stream = container.CreateFile(filename);
                XmlSerializer serializer = new XmlSerializer(typeof(SaveGame));
                serializer.Serialize(stream, SaveData);
                stream.Close();
                container.Dispose();
            }
            result.AsyncWaitHandle.Close();
        }
    }

但我仍然得到相同的结果。我究竟做错了什么?

4

1 回答 1

1

这篇文章中的人遇到了同样的问题,这是由于 using 语句范围之外的某种异常处理导致无法正确调用 dispose 引起的。尝试将您的 using 语句包装在 try catch 中。

另外,我发现这篇文章在寻找解决方案时很有帮助。

祝你好运。

于 2013-01-01T01:55:26.493 回答