WPF - 通过其 FlowDocument 对富文本格式 (RTF) 数据进行简单序列化
我有一个名为 Location:
[Serializable()]
public class Location
{
public int id { get; set; }
public double latitude { get; set; }
public double longitude { get; set; }
public string name { get; set; }
public bool isAnOption { get; set; }
public double distanceFromYaelsParents { get; set; }
public double distanceFromGabrielsParents { get; set; }
public FlowDocument notes { get; set; }
}
notes
的类(类型为 FlowDocument
),它获取窗口上 WPF RichTextBox 的内容。
我想使用简单的序列化来将从此类创建的对象保存到二进制文件(然后读取它们)。假设该项目名为 location
:
using (Stream stream = File.Open(dataFileName, FileMode.Create))
{
BinaryFormatter bin = new BinaryFormatter();
bin.Serialize(stream, location);
}
因此,只要不包含 FlowDocument
,一切都很好。我似乎无法序列化那个。
能做到吗?或者 - 是否有更好的方法以二进制(不是 XAML)保存和读取包含图像和格式化文本的 RichTextBox 的内容?
请详细说明,我对这些东西很陌生。
谢谢
I have a class called Location:
[Serializable()]
public class Location
{
public int id { get; set; }
public double latitude { get; set; }
public double longitude { get; set; }
public string name { get; set; }
public bool isAnOption { get; set; }
public double distanceFromYaelsParents { get; set; }
public double distanceFromGabrielsParents { get; set; }
public FlowDocument notes { get; set; }
}
notes
(of type FlowDocument
) takes the contents of a WPF RichTextBox I have on my window.
I want to use a simple serialization in order to save objects created from this class to a binary file (and later read them). Let's say the item is called location
:
using (Stream stream = File.Open(dataFileName, FileMode.Create))
{
BinaryFormatter bin = new BinaryFormatter();
bin.Serialize(stream, location);
}
So, as long as the FlowDocument
is not included, everything is cool. I don't seem to manage serializing that one.
Can it be done? or alternatively - is there a better way to binary (not XAML) save and read the contents of a RichTextBox with images and formatted text in it?
Please elaborate, I'm pretty new to these things.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
FlowDocument 不可序列化。请参阅 David Ward 对此 StackOverflow 问题的回答,了解可能的解决方案。
基本思想:将FlowDocument转换为XAML(XML)并序列化。
在您的情况下,我将从序列化中排除 FlowDocument 属性,而是使用一个在 getter/setter 中与 FlowDocument 相互转换的字符串属性。
FlowDocument is not serializable. See David Ward's answer to this StackOverflow question for a possible solution.
Basic idea: convert the FlowDocument to XAML (XML) and serialize it.
In your case, I'd exclude the FlowDocument property from serialization and instead have a string property which converts to/from FlowDocument in the getter/setter.