XNA:将对象序列化到隔离存储

发布于 2025-01-06 15:28:56 字数 238 浏览 3 评论 0原文

我有一门课程来定义我的水平。我的内容项目中有一个 XML 文件,用于存储关卡的详细信息。

我可以使用 LoadContent() 从内容项目加载 XML 文件的详细信息。这将创建一个包含 XML 文件中所有详细信息的对象。

现在,我想将该游戏关卡保存到独立存储中。

我见过的所有示例都表明我需要使用 XMLWriter 和 XMLSerializer。这是为什么?我可以不使用 XNA 框架从内容管道加载的机制吗?

I have a class that defines my level. I have an XML file in my Content Project that stores the details of the level.

I can load the details of the XML file from the Content Project with LoadContent(). This creates an object with all the details from the XML file.

Now, I want to save that game level into isolated storage.

All the examples that I've seen, indicate that I need to use XMLWriter and XMLSerializer. Why is that? Can I not use the mechanism that the XNA framework uses to load from the Content Pipeline?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一个人的旅程 2025-01-13 15:28:56

您不需要 XMLWriter 或 XMLSerializer,但确实需要序列化程序。
下面是我的通用隔离存储实用程序的示例。

public static void Save<T>(string fileName, T item)
{
    using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
    {
        using (IsolatedStorageFileStream fileStream = new IsolatedStorageFileStream(fileName, FileMode.Create, storage))
        {
            DataContractSerializer serializer = new DataContractSerializer(typeof(T));
            serializer.WriteObject(fileStream, item);
        }
    }
}

public static T Load<T>(string fileName)
{
    using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
    {
        using (IsolatedStorageFileStream fileStream = new IsolatedStorageFileStream(fileName, FileMode.Open, storage))
        {
            DataContractSerializer serializer = new DataContractSerializer(typeof(T));
            return (T)serializer.ReadObject(fileStream);
        }
    }
}

将 XML 引用为 XNA 内容时,它会通过 ContentPipeline 进行编译。因此,当您加载内容时,您是通过 ContentManager 来完成的。引用的此 XML 文件不应该位于 ContentPipeline 中,因为这样就无法对其进行修改。您应该保留通过 ContentPipline 引用的静态文件,并将所有动态文件保存在isolatedStorage 中。文件一旦编译就无法更改,这就是它无法保存到 ContentPipeline 的原因。

You don't need an XMLWriter or XMLSerializer, but you do need a serializer.
Below is an example of a my Generic IsolatedStorage Utilty

public static void Save<T>(string fileName, T item)
{
    using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
    {
        using (IsolatedStorageFileStream fileStream = new IsolatedStorageFileStream(fileName, FileMode.Create, storage))
        {
            DataContractSerializer serializer = new DataContractSerializer(typeof(T));
            serializer.WriteObject(fileStream, item);
        }
    }
}

public static T Load<T>(string fileName)
{
    using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
    {
        using (IsolatedStorageFileStream fileStream = new IsolatedStorageFileStream(fileName, FileMode.Open, storage))
        {
            DataContractSerializer serializer = new DataContractSerializer(typeof(T));
            return (T)serializer.ReadObject(fileStream);
        }
    }
}

When YOU reference the XML as an XNA content it is compiled throught the ContentPipeline. So when you load the Content you do it through the ContentManager. This XML file referenced should NOT be in the ContentPipeline because then it cannot be modified. You should leave Static files referenced through the ContentPipline and leave all Dynamic files saved in IsolatedStorage. Once files are comiled they cannot be changed thats why it cant be saved to the ContentPipeline.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文