如何在PathGeometry中使用来自资源的PathFigure?

bak*_*a12 1 wpf resources xaml pathgeometry

我想知道是否有任何方法可以在xaml中使用元素中的PathFigure资源PathGeometry.假设我有一个ResourceDictionary,其中有两个PathFigure对象,'x:Key'属性分别等于"Figure1"和"Figure2".我的目标是创建一个PathGeometry带有Figures包含图1和图2的集合的属性.我可以使用代码隐藏文件轻松地完成它,但是我想知道有没有办法只使用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>
Run Code Online (Sandbox Code Playgroud)

我现在可以创建一个PathGeometry:

<PathGeometry FillRule="Nonzero" x:Key="1">
    <PathFigureCollection>
        //Here I want to put Figure1 and Figure2
    </PathFigureCollection>
</PathGeometry>
Run Code Online (Sandbox Code Playgroud)

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

Sza*_*zsi 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>
Run Code Online (Sandbox Code Playgroud)

用法:

<Path Stroke="Black" StrokeThickness="1" Data="{StaticResource OneAndTwo}" />
Run Code Online (Sandbox Code Playgroud)