WPF,将 Path.DataProperty 转换为 Segment 对象
我想知道是否有一个工具可以将“M 0 0 l 10 10”之类的路径数据转换为等效的直线/曲线段代码。
目前我正在使用:
string pathXaml = "<Path xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\" Data=\"M 0 0 l 10 10\"/>";
Path path = (Path)System.Windows.Markup.XamlReader.Load(pathXaml);
在我看来,调用 XamlParser 比显式创建线段慢得多。然而,手动转换大量路径是非常繁琐的。
I was wondering if there was a tool to convert a path data like "M 0 0 l 10 10" to it's equivalent line/curve segment code.
Currently I'm using:
string pathXaml = "<Path xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\" Data=\"M 0 0 l 10 10\"/>";
Path path = (Path)System.Windows.Markup.XamlReader.Load(pathXaml);
It appears to me that calling XamlParser is much slower than explicitly creating the line segments. However converting a lot of paths by hand is very tedious.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
该程序将进行转换:
http://stringtopathgeometry.codeplex.com/
This program will do the conversion:
http://stringtopathgeometry.codeplex.com/
没有任何内置功能可以从几何迷你语言生成 C# 或 VB 代码,但您可以按如下方式创建一个代码:
PathFigureCollection.Parse
。这将返回一个 PathFigureCollection 实例。这是否比手动转换路径更繁琐还是更少,只有您可以决定,不过...这可能取决于您需要处理多少种不同类型的段(即,您的路径中出现了多少种不同类型的段)字符串),因为您必须为 LineSegments、ArcSegments 等编写单独的代码。
编辑:感谢评论中的 Anvaka 通过将我的注意力吸引到 PathFigureCollection.Parse 来简化原始答案。
There's nothing built-in to generate C# or VB code from the geometry minilanguage, but you could create one as follows:
PathFigureCollection.Parse
on your path string. This will return aPathFigureCollection
instance.Whether this is more or less tedious than converting the paths by hand is something only you can decide, though... it probably depends on how many different kinds of segment you need to handle (i.e. how many different kinds of segment appear in your path strings), since you will have to write separate code for LineSegments, ArcSegments, etc.
EDIT: Thanks to Anvaka in comments for simplifying the original answer by drawing my attention to PathFigureCollection.Parse.