需要"Prettify"Xaml:将一个长的PathGeometry"Figures"属性字符串转换为XML语法

Need to "Prettify" Xaml: Convert a long PathGeometry "Figures" attribute string to XML syntax

我在我的应用程序中使用了很多图标。我将它们放在 PathGeometry 对象的 XAML 资源字典中,其中的数字在属性语法中指定,如下所示:

<PathGeometry x:Key="RunPathGeometry" 
              Figures="M56.11 46.17 69.2 33.09a25.29 25.29 0 1 0 4.41 27H85.16l.1 0q-.25.91-.57 1.8A36.32 36.32 0 1 1 77 25.3l8.92-8.92V46.17Z" 
              FillRule="NonZero"
              />

现在这工作得很好,但其中一些图标太复杂,属性字符串太长,以至于我有——实际上——超过 30,000 列文本。

我非常希望用 属性 语法表达这些,如果只是因为它会给我大量的 而不是 。我可以接受的线路(而且肯定会少于 30,000 条......)

有没有人知道一些简单的 method/converter 可以为我做这件事?也许我可以在哪里以一种格式粘贴 PathGeometry 并以另一种格式将其取出?

浏览文档,看起来我可能会用 XamlReader/XamlWriter 自己写一个,但如果有人已经...

已编辑——因为有人问,我所说的 "Property" 语法是指数字用封闭的 XML 标记而不是属性表示的地方。像下面这个例子(它实际上是一个简单的三角形例子,但也可以表达更复杂的形状。)

<Path Stroke="Black" StrokeThickness="1">
    <Path.Data>
        <PathGeometry>
            <PathGeometry.Figures>
                <PathFigureCollection>
                    <PathFigure IsClosed="True" StartPoint="10,100">
                        <PathFigure.Segments>
                            <PathSegmentCollection>
                                <LineSegment Point="100,100" />
                                <LineSegment Point="100,50" />
                            </PathSegmentCollection>
                        </PathFigure.Segments>
                    </PathFigure>
                </PathFigureCollection>
            </PathGeometry.Figures>
        </PathGeometry>
    </Path.Data>
</Path>

我们在资源字典中有很多这样的资源,我们对此非常满意:

<PathGeometry x:Key="SearchGlyphPathGeometry" x:Shared="True" FillRule="EvenOdd">
    <PathGeometry.Figures>
        M 25,13
        A 12,12, 0,1,1, 13,25
        A 12,12, 0,0,1, 25,13
        Z
        M 35.8,41.8
        A 20,20, 0,0,1, 5,25
        A 20,20, 0,1,1, 41.8,35.8
        L 57,51
        L 51,57
        Z
    </PathGeometry.Figures>
</PathGeometry>

这样使用:

<Path Data="{StaticResource SearchGlyphPathGeometry}" Fill="DeepSkyBlue" />

使用 perl 或 sed 在路径字符串中的每个字母字符前插入换行符就足够简单了。或者 C#:

var re = new Regex("([a-z])", RegexOptions.IgnoreCase);
var s = re.Replace(PathGeometryText, "\n\t\t\t");

您可以编写代码以递归方式在项目目录中搜索 XAML 文件,查找 PathGeometry 元素,使用上述正则表达式转换 Figures 属性值,删除属性,并添加一个子元素:

<PathGeometry x:Key="RunPathGeometry" FillRule="NonZero">
    <PathGeometry.Figures>

        M56.11 46.17 69.2 33.09
        a25.29 25.29 0 1 0 4.41 27
        H85.16
        l.1 0
        q-.25.91-.57 1.8
        A36.32 36.32 0 1 1 77 25.3
        l8.92-8.92
        V46.17
        Z 
    </PathGeometry.Figures>
</PathGeometry>

我还会 do something about that mildly annoying orphan newline 正则表达式留在文本前面。

对于您最初的想法,您可以编写一些简单的 C# 代码来从您的字符串中实例化 PathGeometry 个实例并将它们序列化为 XAML,但在我看来,以上内容至少具有可读性,而且工作更少。