有没有办法格式化保存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时添加格式?感谢。
答案 0 :(得分:0)
尝试使用XmlTextWriter
:
using (FileStream fs = File.Create(fileName)
{
XmlTextWriter writer = new XmlTextWriter(fs, Encoding.Default);
writer.Formatting = Formatting.Indented;
textRange.Save(writer, DataFormats.Xaml);
}
这似乎产生了格式化的XML文档。
using (MemoryStream ms = new MemoryStream())
{
textRange.Save(ms, DataFormats.Xaml);
ms.Position = 0;
StreamReader sr = new StreamReader(ms);
XmlDocument doc = new XmlDocument();
XmlTextWriter writer = new XmlTextWriter(fileName, Encoding.Default);
writer.Formatting = Formatting.Indented;
doc.LoadXml(sr.ReadLine());
doc.Save(writer);
writer.Close();
}