开发者

WPF - Simple Serialization of Rich Text Format (RTF) Data through its FlowDocument

开发者 https://www.devze.com 2023-03-15 19:09 出处:网络
I have a class called Location: [Serializable()] public class Location { public int id { get; set; } public double latitude { get; set; }

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.开发者_JS百科

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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号