WPF,将 Path.DataProperty 转换为 Segment 对象

发布于 2024-08-25 16:40:31 字数 408 浏览 15 评论 0原文

我想知道是否有一个工具可以将“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 技术交流群。

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

发布评论

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

评论(2

不必了 2024-09-01 16:40:31

This program will do the conversion:
http://stringtopathgeometry.codeplex.com/

玩套路吗 2024-09-01 16:40:31

没有任何内置功能可以从几何迷你语言生成 C# 或 VB 代码,但您可以按如下方式创建一个代码:

  • 发出 C# 或 VB 代码以新建 PathGeometry。
  • 在路径字符串上调用 PathFigureCollection.Parse 。这将返回一个 PathFigureCollection 实例。
  • 迭代 PathFigureCollection。对于每个数字:
    • 编写 C# 或 VB 代码来新建 PathFigure 对象并将其添加到 PathGeometry.Figures 集合中。
    • 迭代人物的 Segments 集合。对于每个段,分析其类型并发出与类型相关的代码,以新建适当类型的 PathSegment、设置其属性并将其添加到当前 PathFigure。

这是否比手动转换路径更繁琐还是更少,只有您可以决定,不过...这可能取决于您需要处理多少种不同类型的段(即,您的路径中出现了多少种不同类型的段)字符串),因为您必须为 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:

  • Emit C# or VB code for new-ing up a PathGeometry.
  • Call PathFigureCollection.Parse on your path string. This will return a PathFigureCollection instance.
  • Iterate over the PathFigureCollection. For each figure:
    • Write out C# or VB code for new-ing a PathFigure object and adding it to the PathGeometry.Figures collection.
    • Iterate over the figure's Segments collection. For each segment, analyse its type and emit type-dependent code for new-ing up the appropriate kind of PathSegment, setting its properties and adding it to the current PathFigure.

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.

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