问题描述
追踪如何以编程方式将路径字符串转换为WPF中的路径对象并不难,但有没有内置函数可将几何或路径转换回迷你语言中的字符串?
编辑:刚才看到这个,我认为应该有一个叫做其中应该能够做到这一点,事实上也是如此。只需创建其中一个并使用。
您可以使用 XamlWriter
类将对象输出为XAML,几何图形将自动缩小为迷你语言。
例如如果这是您的输入:
< DrawingImage x:Name =segmentsDrawing>
< DrawingImage.Drawing>
< DrawingGroup>
< GeometryDrawing Brush =Red>
< GeometryDrawing.Pen>
< GeometryDrawing.Geometry>
< PathGeometry>
< PathFigure StartPoint =100,100>
< PathFigure.Segments>
< LineSegment Point =100,0/>
< ArcSegment Point =186.6,150SweepDirection =ClockwiseSize =100,100/>
< LineSegment Point =100,100/>
< / PathFigure>
< / PathGeometry>
< /GeometryDrawing.Geometry>
< / GeometryDrawing>
< GeometryDrawing Brush =Blue>
< GeometryDrawing.Pen>
< GeometryDrawing.Geometry>
< PathGeometry>
< PathFigure StartPoint =100,100>
< PathFigure.Segments>
< LineSegment Point =186.6,150/>
< ArcSegment Point =13.4,150SweepDirection =ClockwiseSize =100,100/>
< LineSegment Point =100,100/>
< / PathFigure>
< / PathGeometry>
< /GeometryDrawing.Geometry>
< / GeometryDrawing>
< GeometryDrawing Brush =Green>
< GeometryDrawing.Pen>
< GeometryDrawing.Geometry>
< PathGeometry>
< PathFigure StartPoint =100,100>
< PathFigure.Segments>
< LineSegment Point =13.4,150/>
< ArcSegment Point =100,0SweepDirection =ClockwiseSize =100,100/>
< LineSegment Point =100,100/>
< / PathFigure>
< / PathGeometry>
< /GeometryDrawing.Geometry>
< / GeometryDrawing>
< / DrawingGroup>
< / DrawingImage>
...并且您序列化它...
$ b $我们可以使用XmlTextWriter(新的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>
< GeometryDrawing.Geometry>
< PathGeometry Figures =M100,100L100,0A100,100,0,0,1,186.6,150L100,100/>
< /GeometryDrawing.Geometry>
< / GeometryDrawing>
< GeometryDrawing Brush =#FF0000FF>
< 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>
< GeometryDrawing.Geometry>
< PathGeometry Figures =M100,100L13.4,150A100,100,0,0,1,100,0L100,100/>
< /GeometryDrawing.Geometry>
< / GeometryDrawing>
< / DrawingGroup>
< / DrawingImage>
所有 PathGeometry
语言。如果你想在你的应用程序中使用它,我想你可以把它写到 MemoryStream
并通过创建一个 XmlDocument
。
It's not too hard to track down how to programmatically convert path strings into path objects in WPF, but is there a built-in function to convert a geometry or path back to a string in the mini-language?
Edit: Looking at this just now i thought that there should be a class called GeometryConverter
which should be able to do this, and indeed there is. Just create one of those and use ConvertToString
on the geometry you want to convert.
You can use the XamlWriter
class to output objects as XAML, geometry will automatically be reduced to the mini-language.
e.g. if this is your input:
<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>
...and you serialize it...
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);
...you get the following:
<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>
All the PathGeometry
is now in mini-language. If you want to use this right away in your application i suppose you could write it to a MemoryStream
and get the data from it by creating a XmlDocument
from it.
这篇关于将几何/路径转换为Minilanguage字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!