FlowDocumentをバイト配列にシリアル化および逆シリアル化して保存するためにとを使用していXamlWriter.Save
ます。XamlReader.Load
private static byte[] FlowDocumentToByteArray(FlowDocument flowDocument)
{
using (var stream = new MemoryStream())
{
XamlWriter.Save(flowDocument, stream);
stream.Position = 0;
return stream.ToArray();
}
}
private static FlowDocument FlowDocumentFromByteArray(byte[] bytes)
{
using (var stream = new MemoryStream(bytes))
{
return (FlowDocument)XamlReader.Load(stream);
}
}
ただし、空白の保存に問題があります。次のテストは失敗します。
[TestMethod]
public void FlowDocumentTabsNoBackground()
{
var flowDocument = new FlowDocument();
var paragraph = new Paragraph();
paragraph.Inlines.Add(new Run("\t"));
paragraph.Inlines.Add(new Run("a"));
flowDocument.Blocks.Add(paragraph);
byte[] bytes = FlowDocumentToByteArray(flowDocument);
FlowDocument flowDocumentOut = FlowDocumentFromByteArray(bytes);
var textRange = new TextRange(flowDocumentOut.ContentStart,
flowDocumentOut.ContentEnd);
Assert.IsTrue(textRange.Text.StartsWith("\t"));
}
しかし、このテストは合格です:
[TestMethod]
public void FlowDocumentTabsWithBackground()
{
var flowDocument = new FlowDocument();
var paragraph = new Paragraph();
paragraph.Inlines.Add(new Run("\t"));
paragraph.Inlines.Add(new Run("a")
{
Background = new SolidColorBrush(Color.FromArgb(255, 255, 255, 255))
}
);
flowDocument.Blocks.Add(paragraph);
byte[] bytes = FlowDocumentToByteArray(flowDocument);
FlowDocument flowDocumentOut = FlowDocumentFromByteArray(bytes);
var textRange = new TextRange(flowDocumentOut.ContentStart,
flowDocumentOut.ContentEnd);
Assert.IsTrue(textRange.Text.StartsWith("\t"));
}
タブがシリアル化/逆シリアル化のサイクルを通過しないのはなぜですか?また、段落内の実行の1つに背景色を追加すると役立つのはなぜですか?