已保存 FlowDocument 的 XAML 的换行和缩进?

发布于 2024-09-11 20:21:48 字数 2657 浏览 3 评论 0原文

有没有办法格式化保存 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 技术交流群。

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

发布评论

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

评论(1

下雨或天晴 2024-09-18 20:21:48

尝试使用 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();
}

Try using an XmlTextWriter:

using (FileStream fs = File.Create(fileName)
{
    XmlTextWriter writer = new XmlTextWriter(fs, Encoding.Default);
    writer.Formatting = Formatting.Indented;
    textRange.Save(writer, DataFormats.Xaml);
}

Second Attempt:

This seems to produce a formatted XML document.

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