4

我的问题很简单。

如何转换此代码:

<Path>
    <Path.Data>
        <EllipseGeometry Center="5,4" RadiusX="4" RadiusY="4"/>
    </Path.Data>
</Path>

变成类似的东西

<Path Data="M 0 5 L 3 10 10 0"/>

(请注意,第二个产生一个复选标记,而不是一个椭圆。这只是为了说明目的,我的目标就是:找出什么序列给出一个椭圆)

编辑:我还阅读了有关 xaml 中贝塞尔曲线的文档,并且完全意识到我可以通过计算贝塞尔曲线的确切点来简单地生成正确的代码以获得椭圆,但我不想去麻烦做这个计算我自己,所以我想知道是否有一种简单的方法可以做到这一点(也许在 Blend 中)

4

1 回答 1

9

这显示了如何从代码中做到这一点。我不确定这是否能解决你的问题 - 你说“混合?” 在标题中,我不知道在 Blend 中执行此操作的方法。但我希望这可能会有所帮助。

第一步是将 转换EllipseGeometry为 a PathGeometry

var geom = new EllipseGeometry(new Point(5, 4), 4, 4);
var pathGeometry = PathGeometry.CreateFromGeometry(geom);

一旦你有一个PathGeometry你可以简单地打电话ToString,它会给你字符串表示:

string pathText = pathGeometry.ToString();

在我的系统上,这会产生以下相当冗长的文本:

“M9,4C9,6.20913899932317 7.209389323,885,8.209999323,885,8.20919993,05,87.20919993,05,87.20938993,05,8,8.2091989323,878,87.209389837688,8,8.2091389932317067839323170678393768,87.209.0683837.>>>>>>>>>>>>>

现在,如果您想最终得到将字符串输入 Xaml 时会得到的结果,则需要多一步,因为 Xaml 是您提供的表单:

<Path Data="M 0 5 L 3 10 10 0"/>

doesn't produce a PathGeometry. It produces a StreamGeometry, which is a slightly more efficient, but fixed representation of the path. (The main difference being that you can't get hold of individual objects representing the various figures and segements in a StreamGeometry, whereas you can with a PathGeometry. StreamGeometry is cheaper, as a result.)

You can get a StreamGeometry from the path text:

var streamGeometry = StreamGeometry.Parse(pathText);

And then if you want that in a Path just build it:

var p = new Path { Data = streamGeometry };

Depending on what your exact goal is, there may be more efficient ways to do this. (StreamGeometry is a more efficient representation because it ends up creating far fewer objects. But the route by which my code gets to that representation is not very efficient, so you might be better off just stopping with the PathGeometry that the very first code snippet produces. Alternatively, if your goal is really just to get the path text, then the PathGeometry is also sufficient for your needs.)

于 2010-12-14T10:57:34.913 回答