6

我对一个简单的 PathGeometry 对象有一个奇怪的错误,我似乎无法弄清楚。如果有人可以向我解释为什么这不起作用,我将不胜感激。

这是一个工作路径的示例,它绘制了一个小三角形:

<Path Data="M 8,4 L 12,12 4,12 8,4 Z" Stroke="White" />

这是一个似乎对我不起作用的路径示例:

<Path Stroke="White">
    <Path.Data>
        <PathGeometry Figures="M 8,4 L 12,12 4,12 8,4 Z" />
    </Path.Data>
</Path>

Data 和 Figures 属性中的字符串是相同的,但后一个示例会导致异常:

属性数字的无效属性值 M 8,4 L 12,12 4,12 8,4 Z。

我最终想做的是将 PathGeometry 放入 ResourceDictionary 并将其引用为 {StaticResource} 以便我可以重用我的形状。

编辑:

我的解决方案是不尝试使用 StaticResource 引用 PathGeometry,而是引用字符串资源。

<sys:String x:Key="TriangleShape">M 8,4 L 12,12 4,12 8,4 Z</sys:String>
...
<Path Data={StaticResource TriangleShape}" />
4

1 回答 1

4

据我所知, PathGeometry不支持 Path.Data使用的路径标记语法。PathGeometry.Figures属性必须是 PathFigure 对象的集合

要以这种方式指定上述形状,您可以执行以下操作:

    <Path Stroke="White">
        <Path.Data>
            <PathGeometry>
                <PathGeometry.Figures>
                    <PathFigure StartPoint="8,4">
                        <PathFigure.Segments>
                            <LineSegment Point="12,12" />
                            <LineSegment Point="4,12" />
                            <LineSegment Point="8,4" />
                        </PathFigure.Segments>
                    </PathFigure>
                </PathGeometry.Figures>
            </PathGeometry> 
        </Path.Data>
    </Path>

免责声明:我没有在 WP7 上尝试过,只在我的 PC 上的 Silverlight 上尝试过。

于 2011-01-29T18:34:23.467 回答