如何保存类的实例并再次读取它 [英] How to save an instance of a class and the read it again

查看:96
本文介绍了如何保存类的实例并再次读取它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含5个属性的类:3个字符串,1个Brush 和1个DateTime,我正在尝试找到保存此类实例的方法,但我不断收到序列化错误?

Hi, I have a class which contains 5 properties: 3 strings, one Brush and one DateTime, and i'm trying to find a way to save an instance of this class but I keep getting serialization errors?

此外,我想以这样的方式保存它,以便我可以获取此类的所有已保存实例并将其显示在gridview中。 (顺便说一下这是我正在谈论的地铁风格的应用程序)

Also I want to save it in such a way so that I can get all saved instances of this class and display them in a gridview. (btw this is a metro style app i'm talking about)

如果有人能解释如何做到这一点,我将非常感激!

If someone could explain how to do this I would be extremely grateful!

谢谢

Dominic Ayre

Dominic Ayre

推荐答案

嘿,在VS 11的项目模板中,有一个类叫SuspensionManager。它使用字典< string,object>处理会话状态。您可能只能使用此管理器来保存对象的不同实例。

Hey in the project template of VS 11, there is a class called SuspensionManager. It deals with session state using a dictionary <string, object>. You might just be able to use this manager to save different instances of your objects.

我所指的方法是:

        /// <summary>
        /// Save the current <see cref="SessionState"/>.  Any <see cref="Frame"/> instances
        /// registered with <see cref="RegisterFrame"/> will also preserve their current
        /// navigation stack, which in turn gives their active <see cref="Page"/> an opportunity
        /// to save its state.
        /// </summary>
        /// <returns>An asynchronous task that reflects when session state has been saved.</returns>
        public static async Task SaveAsync()


        /// <summary>
        /// Restores previously saved <see cref="SessionState"/>.  Any <see cref="Frame"/> instances
        /// registered with <see cref="RegisterFrame"/> will also restore their prior navigation
        /// state, which in turn gives their active <see cref="Page"/> an opportunity restore its
        /// state.
        /// </summary>
        /// <returns>An asynchronous task that reflects when session state has been read.  The
        /// content of <see cref="SessionState"/> should not be relied upon until this task
        /// completes.</returns>
        public static async Task RestoreAsync()


但是,只需确保将类型包含在暂停管理器中的KnowTypes(List< Type>)属性中。

However, just make sure to include your type in the KnowTypes (List<Type>) property in the suspension manager.


这篇关于如何保存类的实例并再次读取它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆