Wpf 如何防止XamlReader.Load(xamlFile)插入额外的运行元素?

Wpf 如何防止XamlReader.Load(xamlFile)插入额外的运行元素?,wpf,flowdocument,xamlreader,flowdocumentreader,Wpf,Flowdocument,Xamlreader,Flowdocumentreader,假设我有这样一个流程文档: <FlowDocument xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" ColumnWidth="400" FontSize="14" FontFamily="Georgia" Name="document"> <Paragraph KeepTogether="True"> &l

假设我有这样一个流程文档:

<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时,我看到它在它们之间插入了一条基本上有一个空格作为内容的运行,因此在段落中没有我期望的3条内联线,而是有5条

如何避免插入这些空白运行

注意:我不能将这三个运行组合成一个单一的运行,这将是一个简单的答案,因为它们需要保持独立的对象

想法



解决方案:正如Aaron在下面正确回答的那样,将所有运行分组到一行可以解决此问题。另一方面,本文档是根据其他数据动态构建的(比我的示例更复杂),并使用XmlWriter编写,该XmlWriter将XmlWriterSettings属性Indent设置为true(因为更容易看到输出,而不是同时运行)-当XamlReader读入时,将其设置为false可消除这些额外的跑步记录。

您只需将所有跑步记录放在一行即可

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

对问题投了赞成票;谁会想到XAML:

<TextBlock.Inlines><Run Text="[" /><Run Text="0" /><Run Text="-" /><Run Text="2" /><Run Text="]" /></TextBlock.Inlines>

将产生与以下不同的结果:

<TextBlock.Inlines>
    <Run Text="[" />
    <Run Text="0" />
    <Run Text="-" />
    <Run Text="2" />
    <Run Text="]" />
</TextBlock.Inlines>

第一个给出“[0-2]”,第二个给出“[0-2]”。特别要注意的是,在第二种情况下,第一个和最后一个
运行也会得到特殊处理,因为在它们之前(或之后)没有插入空格


显然,
内联
为其
运行
子项获取原始XAML的便利特性并没有失败,即使明确提供了(至少一个)
内联

太好了,这会解决问题的。
<TextBlock.Inlines>
    <Run Text="[" />
    <Run Text="0" />
    <Run Text="-" />
    <Run Text="2" />
    <Run Text="]" />
</TextBlock.Inlines>