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

发布于 2024-10-17 03:52:00 字数 1334 浏览 1 评论 0原文

想象一下我有一个像这样的 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,这些 Runs 本质上有一个空格作为其内容,因此段落中没有我期望的 3 个内联,而是 5 个。

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

注意:我无法真正将这三个运行分组为一个运行,这将是一个简单的答案,因为它们需要保持独立的对象。

有想法吗?


解决方案:正如亚伦在下面正确回答的那样,将所有运行分组在一行上可以解决此问题。顺便说一句,该文档是根据其他数据即时构建的(比我的示例更复杂),并使用 XmlWriter 编写,该 XmlWriterSettings 属性 Indent 设置为 true(因为更容易查看输出而不是全部)一起运行) - 将其设置为 false 可消除 XamlReader 读入时的额外运行。

Imagine I've got a FlowDocument like so:

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

And I load it like so:

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

What results is this:

enter image description here

While what I'd like to see is something like so:

enter image description here

but upon examining the FlowDocument in the watch window, I see that it inserts Runs between them that essentially have a space as their content, so rather than there being the 3 Inlines that I expect inside the Paragraph, there are 5.

How can I avoid those blank runs being inserted?

Note: I can't really group those three runs into a single one, which would be the easy answer, due to the fact that they need to remain separate objects.

Ideas?


Solution: As Aaron correctly answered below, grouping the Runs all on one line fixes this issue. As an aside, this document was being built on the fly from other data (more complex than my example) and written out using an XmlWriter that had the XmlWriterSettings property Indent set to true (because it was easier to see the output rather than it all running together) - setting it to false eliminates those extra Runs when it is read in by the XamlReader.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

晨曦÷微暖 2024-10-24 03:52:00

您可以简单地将所有运行放在一行上...

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

然后您可以通过 InlineCollection 访问每个运行...

foreach (var run in ((Paragraph)content.Blocks.FirstBlock).Inlines)
{
   //do stuff with run             
}

You can simply place all your runs on a single line...

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

You can then access each run via the InlineCollection...

foreach (var run in ((Paragraph)content.Blocks.FirstBlock).Inlines)
{
   //do stuff with run             
}
吃素的狼 2024-10-24 03:52:00

对问题投了赞成票;谁会想到 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]”。请特别注意,在第二种情况下,第一个和最后一个 Run 也会得到特殊处理,因为在它们之前(或之后)没有插入空格。

显然,Inline 为其 Run 子级获取原始 XAML 的便利功能并没有被击败,即使(至少一个)Inline 被显式指定时也是如此。假如。

Upvoted the question; who would have guessed that the XAML:

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

would give different result from:

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

The first gives "[0-2]" while the second gives "[ 0 - 2 ]". Note, especially, that in the second case, the first and last Run also get special treatment, since there is no space inserted before (or after) those.

Obviously, the convenience feature where the Inline grabs raw XAML for its Run children is not defeated, even when (at least one) Inline is explicitly provided.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文