在WPF中自动关闭应用程序后,恢复旧状态的应用程序

时间:2014-03-19 05:51:31

标签: c# wpf

如何在自动关闭应用程序时保存WPF中的页面状态,以便在再次运行应用程序时,显示相同旧状态的同一页面?

1 个答案:

答案 0 :(得分:0)

正如郭所说,使用序列化。例如:

using using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
[Serializable]
class Example
{
    private int i;
    private void Save(string fileToSave)
    {
        using(Stream writer = File.OpenWrite(fileToSave))
        {
            BinaryFormatter bf = new BinaryFormatter();
            bf.Serialize(writer, this);
        }
    }
    private void Load(string loadFromFile)
    {
        using (Stream reader = File.OpenRead(loadFromFile))
        {
            BinaryFormatter bf = new BinaryFormatter();
            Example temp = (Example)bf.Deserialize(reader);
            //and here you can restore your class from loadFromFile
            this.i = temp.i;
        }
    }
}