1

我想知道是否有任何方法可以在 xamlPathFigure的元素中使用 from 资源。PathGeometry假设我有一个 ResourceDictionary 和两个PathFigure对象,其中的“x:Key”属性分别等于“Figure1”和“Figure2”。我的目标是创建一个PathGeometrywithFigures属性,其中填充了一个包含 Figure1 和 Figure2 的集合。我可以使用隐藏文件的代码轻松地做到这一点,但是我想知道是否有任何方法可以仅使用 xaml 来做到这一点。

<PathFigure IsClosed="True" StartPoint="2,9" x:Key="Figure1">
    <ArcSegment Point="15,9" Size="6.5, 2"/>
    <LineSegment Point="15,12"/>
    <ArcSegment Point="2,12" Size="6.5, 2"/>
</PathFigure>

<PathFigure IsClosed="True" StartPoint="10,7" x:Key="Figure2">
    <LineSegment Point="10, 2"/>
    <LineSegment Point="13,2"/>
    <LineSegment Point="13,7"/>
    <ArcSegment Point="10,7" Size="2,2" IsLargeArc="True"/>
</PathFigure>

我现在可以创建一个PathGeometry

<PathGeometry FillRule="Nonzero" x:Key="1">
    <PathFigureCollection>
        //Here I want to put Figure1 and Figure2
    </PathFigureCollection>
</PathGeometry>

我想我可以写一些MarkupExtension来做以下事情,但我正在寻找最好的方法。感谢您的建议。

4

1 回答 1

5

您不需要任何特别的东西,只需要已经内置的StaticResourceExtension类(标记扩展)。

<Window.Resources>
    <PathFigure IsClosed="True" StartPoint="2,9" x:Key="Figure1">
        <ArcSegment Point="15,9" Size="6.5, 2"/>
        <LineSegment Point="15,12"/>
        <ArcSegment Point="2,12" Size="6.5, 2"/>
    </PathFigure>

    <PathFigure IsClosed="True" StartPoint="10,7" x:Key="Figure2">
        <LineSegment Point="10, 2"/>
        <LineSegment Point="13,2"/>
        <LineSegment Point="13,7"/>
        <ArcSegment Point="10,7" Size="2,2" IsLargeArc="True"/>
    </PathFigure>

    <PathGeometry FillRule="Nonzero" x:Key="OneAndTwo">
        <StaticResource ResourceKey="Figure1" />
        <StaticResource ResourceKey="Figure2" />
    </PathGeometry>
</Window.Resources>

用法:

<Path Stroke="Black" StrokeThickness="1" Data="{StaticResource OneAndTwo}" />
于 2016-02-16T18:51:39.630 回答