想象一下,我有一个这样的 FlowDocument:
<FlowDocument xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
ColumnWidth="400" FontSize="14" FontFamily="Georgia"
Name="document">
<Paragraph KeepTogether="True">
<Run>ABC</Run>
<Run>DEF</Run>
<Run>GHI</Run>
</Paragraph>
</FlowDocument>
我像这样加载它:
private void Button_Click(object sender, RoutedEventArgs e)
{
FileStream xamlFile = new FileStream("FlowDocument1.xaml", FileMode.Open);
FlowDocument content = XamlReader.Load(xamlFile) as FlowDocument;
fdReader.Document = content;
}
这是什么结果:
虽然我想看到的是这样的:
但是在查看监视窗口中的 FlowDocument 时,我看到它在它们之间插入了 Runs,它们的内容本质上是一个空格,因此我在段落中没有我期望的 3 个内联,而是有 5 个。
如何避免插入那些空白运行?
注意:我不能真正将这三个运行分组为一个,这将是一个简单的答案,因为它们需要保持单独的对象。
想法?
解决方案:正如 Aaron 在下面正确回答的那样,将 Runs all 分组在一行上可以解决此问题。顺便说一句,该文档是根据其他数据动态构建的(比我的示例更复杂),并使用 XmlWriterSettings 属性 Indent 设置为 true 的 XmlWriter 写出(因为更容易看到输出而不是全部一起运行)- 将其设置为 false 会在 XamlReader 读入时消除那些额外的运行。