WPF : Can BinaryFormatter serialize FlowDocument instance?

*爱你&永不变心* 提交于 2021-02-19 03:37:28

问题


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);

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

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


回答1:


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;

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

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.



来源:https://stackoverflow.com/questions/6978067/wpf-can-binaryformatter-serialize-flowdocument-instance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!