WPF:BinaryFormatter 可以序列化 FlowDocument 实例吗? [英] WPF : Can BinaryFormatter serialize FlowDocument instance?

查看:76
本文介绍了WPF:BinaryFormatter 可以序列化 FlowDocument 实例吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我喜欢使用 binaryformatter 来序列化流文档.但它例外.

I like to using binaryformatter to serializing flow document. but it makes exception.

[Serializable]
public class BinFlow
{
    public FlowDocument my { get; set; }
}


BinFlow myBF = new BinFlow();
myBF.my = myFlowDocument;

FileStream myFile = File.Create(@"d:\test.bin");
BinaryFormatter myBinaryFormat = new BinaryFormatter();

//exception occured here!!
myBinaryFormat.Serialize(myFile, myBF);

异常消息表示FlowDocument 未取消 'Serializable' 属性".

Exception message said "FlowDocument does not decared 'Serializable' proeprty".

ps.当然,我可以使用 XamlReader 和 XamlWriter 来序列化 FlowDocument.但我认为二进制可以为这项工作提供更快的性能.

ps. Of cause, I can use XamlReader and XamlWriter for serializing of FlowDocument. but I think binary can more fast performance for this work.

推荐答案

我假设您在询问相关问题 - 将 FlowDocument 从一个线程传送到另一个线程.我使用 BinaryFormatter 从未取得任何成功.除非您的 FlowDocument 非常大(例如,超过 100 MB),否则您可以轻松地将其保存到内存中,以便在线程之间作为内存流共享,如下所示:

I am assuming you are asking for your related question -- to shuttle your FlowDocument from one thread to another. I've never had any success using BinaryFormatter. Unless your FlowDocument is very large (say, more than 100 MB), you can easily save it to memory to share between threads as a memory stream like this:

MemoryStream stream = new MemoryStream();
XamlWriter.Save(myFlowDoc, stream);
stream.Position = 0;

线程间可以共享MemoryStream,避免磁盘IO.在另一个线程上,使用 MemoryStream 中的 XamlReader.Load.

You can share MemoryStream between threads, and avoid disk IO. On your other thread, use XamlReader.Load from the MemoryStream.

如果您确实想以二进制格式将其写入磁盘,我会说获取 Xaml,然后使用压缩库制作 ZIP 文件,就像 XPS 一样.

If you do want to write it to disk in a binary format I'd say get the Xaml, then use the compression libraries to make a ZIP file, as XPS does.

这篇关于WPF:BinaryFormatter 可以序列化 FlowDocument 实例吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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