已保存 FlowDocument 的 XAML 的换行和缩进?
有没有办法格式化保存 FlowDocument 时生成的 XAML?目前,它全部在一行上运行,我想将其分解为多个元素,并使用换行符和缩进,以使其更易于阅读。
下面是我用来将 WPF RichTextBox 中的 FlowDocument 保存为 XAML 文件的代码:
// Create a TextRange around the entire document
TextRange textRange = new TextRange(myRichTextBox.Document.ContentStart, myRichTextBox.Document.ContentEnd);
// Save file
using (FileStream fs = File.Create(fileName))
{
textRange.Save(fs, DataFormats.Xaml);
}
保存工作正常,但正如我提到的,生成的 XAML 全部一起运行,其各个元素没有缩进或换行符:
<Section xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xml:space="preserve" TextAlignment="Left" LineHeight="Auto" IsHyphenationEnabled="False" xml:lang="en-us" FlowDirection="LeftToRight" NumberSubstitution.CultureSource="Text" NumberSubstitution.Substitution="AsCulture" FontFamily="Calibri" FontStyle="Normal" FontWeight="Normal" FontStretch="Normal" FontSize="12" Foreground="#FF000000" Typography.StandardLigatures="True" Typography.ContextualLigatures="True" Typography.DiscretionaryLigatures="False" Typography.HistoricalLigatures="False" Typography.AnnotationAlternates="0" Typography.ContextualAlternates="True" Typography.HistoricalForms="False" Typography.Kerning="True" Typography.CapitalSpacing="False" Typography.CaseSensitiveForms="False" Typography.StylisticSet1="False" Typography.StylisticSet2="False" Typography.StylisticSet3="False" Typography.StylisticSet4="False" Typography.StylisticSet5="False" Typography.StylisticSet6="False" Typography.StylisticSet7="False" Typography.StylisticSet8="False" Typography.StylisticSet9="False" Typography.StylisticSet10="False" Typography.StylisticSet11="False" Typography.StylisticSet12="False" Typography.StylisticSet13="False" Typography.StylisticSet14="False" Typography.StylisticSet15="False" Typography.StylisticSet16="False" Typography.StylisticSet17="False" Typography.StylisticSet18="False" Typography.StylisticSet19="False" Typography.StylisticSet20="False" Typography.Fraction="Normal" Typography.SlashedZero="False" Typography.MathematicalGreek="False" Typography.EastAsianExpertForms="False" Typography.Variants="Normal" Typography.Capitals="Normal" Typography.NumeralStyle="Normal" Typography.NumeralAlignment="Normal" Typography.EastAsianWidths="Normal" Typography.EastAsianLanguage="Normal" Typography.StandardSwashes="0" Typography.ContextualSwashes="0" Typography.StylisticAlternates="0"><Paragraph NumberSubstitution.CultureSource="User" FontFamily="Segoe UI"><Run>Lorem ipsum dolor si ament</Run></Paragraph></Section>
我希望像传统 XAML 一样在文件中设置 XAML 格式,并为各种元素添加换行符和缩进。有什么方法可以让 .NET 在生成 XAML 时添加格式吗?谢谢。
Is there a way to format the XAML that is generated when a FlowDocument is saved? Currently, it is all run together on one line, and I'd like to break it up into its elements, with line breaks and indenting, to make it a little easier to read.
Here is the code I am using to save a FlowDocument from a WPF RichTextBox as a XAML file:
// Create a TextRange around the entire document
TextRange textRange = new TextRange(myRichTextBox.Document.ContentStart, myRichTextBox.Document.ContentEnd);
// Save file
using (FileStream fs = File.Create(fileName))
{
textRange.Save(fs, DataFormats.Xaml);
}
The save works fine, but as I mentioned, the XAML that is generated is all run together, with no indentation or line breaks for its various elements:
<Section xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xml:space="preserve" TextAlignment="Left" LineHeight="Auto" IsHyphenationEnabled="False" xml:lang="en-us" FlowDirection="LeftToRight" NumberSubstitution.CultureSource="Text" NumberSubstitution.Substitution="AsCulture" FontFamily="Calibri" FontStyle="Normal" FontWeight="Normal" FontStretch="Normal" FontSize="12" Foreground="#FF000000" Typography.StandardLigatures="True" Typography.ContextualLigatures="True" Typography.DiscretionaryLigatures="False" Typography.HistoricalLigatures="False" Typography.AnnotationAlternates="0" Typography.ContextualAlternates="True" Typography.HistoricalForms="False" Typography.Kerning="True" Typography.CapitalSpacing="False" Typography.CaseSensitiveForms="False" Typography.StylisticSet1="False" Typography.StylisticSet2="False" Typography.StylisticSet3="False" Typography.StylisticSet4="False" Typography.StylisticSet5="False" Typography.StylisticSet6="False" Typography.StylisticSet7="False" Typography.StylisticSet8="False" Typography.StylisticSet9="False" Typography.StylisticSet10="False" Typography.StylisticSet11="False" Typography.StylisticSet12="False" Typography.StylisticSet13="False" Typography.StylisticSet14="False" Typography.StylisticSet15="False" Typography.StylisticSet16="False" Typography.StylisticSet17="False" Typography.StylisticSet18="False" Typography.StylisticSet19="False" Typography.StylisticSet20="False" Typography.Fraction="Normal" Typography.SlashedZero="False" Typography.MathematicalGreek="False" Typography.EastAsianExpertForms="False" Typography.Variants="Normal" Typography.Capitals="Normal" Typography.NumeralStyle="Normal" Typography.NumeralAlignment="Normal" Typography.EastAsianWidths="Normal" Typography.EastAsianLanguage="Normal" Typography.StandardSwashes="0" Typography.ContextualSwashes="0" Typography.StylisticAlternates="0"><Paragraph NumberSubstitution.CultureSource="User" FontFamily="Segoe UI"><Run>Lorem ipsum dolor si ament</Run></Paragraph></Section>
I'd like to have the XAML formatted in the file like conventional XAML, with line breaks and indentation for the various elements. Is there any way to have .NET add the formatting as it generates the XAML? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试使用
XmlTextWriter
:第二次尝试:
这似乎会生成一个格式化的 XML 文档。
Try using an
XmlTextWriter
:Second Attempt:
This seems to produce a formatted XML document.