【问题标题】:Convert Geometry/Path to Minilanguage String?将几何/路径转换为 ​​Minilanguage 字符串?
【发布时间】:2011-03-08 02:59:34
【问题描述】:

在 WPF 中如何以编程方式将路径字符串转换为路径对象并不难,但是是否有内置函数可以将几何或路径转换回迷你语言中的字符串?

【问题讨论】:

  • 我猜不是,因为恰恰相反:迷你语言被解析成几何图形。
  • “追踪如何以编程方式将路径字符串转换为 WPF 中的路径对象并不难”愿意分享您的一些资源吗?

标签: wpf xaml geometry mini-language


【解决方案1】:

编辑: 刚才看到这个我认为应该有一个名为GeometryConverter 的类应该能够做到这一点,而且确实有。只需创建其中一个并在要转换的几何图形上使用 ConvertToString


您可以使用XamlWriter 类将对象输出为 XAML,几何图形将自动简化为迷你语言。

例如如果这是您的输入:

<DrawingImage x:Name="segmentsDrawing">
    <DrawingImage.Drawing>
        <DrawingGroup>

            <GeometryDrawing Brush="Red">
                <GeometryDrawing.Pen>
                    <Pen Brush="Black" />
                </GeometryDrawing.Pen>
                <GeometryDrawing.Geometry>
                    <PathGeometry>
                        <PathFigure StartPoint="100,100">
                            <PathFigure.Segments>
                                <LineSegment Point="100,0"/>
                                <ArcSegment Point="186.6,150"  SweepDirection="Clockwise" Size="100,100"/>
                                <LineSegment Point="100,100"/>
                            </PathFigure.Segments>
                        </PathFigure>
                    </PathGeometry>
                </GeometryDrawing.Geometry>
            </GeometryDrawing>

            <GeometryDrawing Brush="Blue">
                <GeometryDrawing.Pen>
                    <Pen Brush="Black"/>
                </GeometryDrawing.Pen>
                <GeometryDrawing.Geometry>
                    <PathGeometry>
                        <PathFigure StartPoint="100,100">
                            <PathFigure.Segments>
                                <LineSegment Point="186.6,150"/>
                                <ArcSegment Point="13.4,150" SweepDirection="Clockwise" Size="100,100"/>
                                <LineSegment Point="100,100"/>
                            </PathFigure.Segments>
                        </PathFigure>
                    </PathGeometry>
                </GeometryDrawing.Geometry>
            </GeometryDrawing>

            <GeometryDrawing Brush="Green">
                <GeometryDrawing.Pen>
                    <Pen Brush="Black"/>
                </GeometryDrawing.Pen>
                <GeometryDrawing.Geometry>
                    <PathGeometry>
                        <PathFigure StartPoint="100,100">
                            <PathFigure.Segments>
                                <LineSegment Point="13.4,150"/>
                                <ArcSegment Point="100,0" SweepDirection="Clockwise" Size="100,100"/>
                                <LineSegment Point="100,100"/>
                            </PathFigure.Segments>
                        </PathFigure>
                    </PathGeometry>
                </GeometryDrawing.Geometry>
            </GeometryDrawing>
        </DrawingGroup>
    </DrawingImage.Drawing>
</DrawingImage>

...然后你序列化它...

XmlTextWriter writer = new XmlTextWriter(@"C:\Users\Public\Test.xml", new UTF8Encoding());
writer.Formatting = Formatting.Indented;
writer.Indentation = 1;
writer.IndentChar = '\t';
XamlWriter.Save(segmentsDrawing, writer);

...你会得到以下结果:

<DrawingImage xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
    <DrawingImage.Drawing>
        <DrawingGroup>
            <DrawingGroup.Children>
                <GeometryDrawing Brush="#FFFF0000">
                    <GeometryDrawing.Pen>
                        <Pen Brush="#FF000000" />
                    </GeometryDrawing.Pen>
                    <GeometryDrawing.Geometry>
                        <PathGeometry Figures="M100,100L100,0A100,100,0,0,1,186.6,150L100,100" />
                    </GeometryDrawing.Geometry>
                </GeometryDrawing>
                <GeometryDrawing Brush="#FF0000FF">
                    <GeometryDrawing.Pen>
                        <Pen Brush="#FF000000" />
                    </GeometryDrawing.Pen>
                    <GeometryDrawing.Geometry>
                        <PathGeometry Figures="M100,100L186.6,150A100,100,0,0,1,13.4,150L100,100" />
                    </GeometryDrawing.Geometry>
                </GeometryDrawing>
                <GeometryDrawing Brush="#FF008000">
                    <GeometryDrawing.Pen>
                        <Pen Brush="#FF000000" />
                    </GeometryDrawing.Pen>
                    <GeometryDrawing.Geometry>
                        <PathGeometry Figures="M100,100L13.4,150A100,100,0,0,1,100,0L100,100" />
                    </GeometryDrawing.Geometry>
                </GeometryDrawing>
            </DrawingGroup.Children>
        </DrawingGroup>
    </DrawingImage.Drawing>
</DrawingImage>

所有PathGeometry 现在都是迷你语言。如果您想在您的应用程序中立即使用它,我想您可以将其写入MemoryStream 并通过从中创建XmlDocument 来获取数据。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-11-23
    • 1970-01-01
    • 1970-01-01
    • 2022-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多