如何防止 XamlReader.Load(xamlFile) 插入额外的 Run 元素?
想象一下我有一个像这样的 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:
While what I'd like to see is something like so:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以简单地将所有运行放在一行上...
然后您可以通过
InlineCollection
访问每个运行...You can simply place all your runs on a single line...
You can then access each run via the
InlineCollection
...对问题投了赞成票;谁会想到 XAML:
会给出不同的结果:
第一个给出“[0-2]”,而第二个给出“[0-2]”。请特别注意,在第二种情况下,第一个和最后一个 Run 也会得到特殊处理,因为在它们之前(或之后)没有插入空格。
显然,
Inline
为其Run
子级获取原始 XAML 的便利功能并没有被击败,即使(至少一个)Inline
被显式指定时也是如此。假如。Upvoted the question; who would have guessed that the XAML:
would give different result from:
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 itsRun
children is not defeated, even when (at least one)Inline
is explicitly provided.