如何将路径和形状的组合作为xaml中图像源的输入

时间:2017-03-20 15:07:02

标签: c# wpf xaml

我有一个svg文件,内容是两组路径和一些多边形,我应该用它们作为图像源的输入。(svg内容:两个gids,它们包含路径和多边形的集合)

我从svg中提取了下面的路径和多边形,并创建了几何组

   <GeometryGroup x:Key="icon-2"
  PresentationOptions:Freeze="True" >
        <PathGeometry Figures="M0.664201681.........." />
        <PathGeometry Figures="M0.664201681.........." />
    <PathGeometry Figures="M0.664201681.........." />
    </GeometryGroup>

    <GeometryGroup x:Key="icon-1"
  PresentationOptions:Freeze="True" >
        <PathGeometry Figures="M0.664201681.........." />
        <PathGeometry Figures="M0.664201681.........." />
    <PathGeometry Figures="M0.664201681.........." />
    </GeometryGroup>

我在svg文件中有一些多边形,如下所示

<polygon id="Shape" fill="#0067B1" points="42.1853782 0.249018403 0.249008405........"></polygon>

我尝试添加上面的多边形,但是我收到错误,说不支持Polygon。

<Style
    x:Key="myLogoStyle"
    TargetType="Image">
    <Setter
        Property="Source">
<Setter.Value>
        <DrawingImage
     PresentationOptions:Freeze="True" >
            <DrawingImage.Drawing>
                <DrawingGroup>
                    <GeometryDrawing Brush="#FFFFFF"
      Geometry="{StaticResource icon-2}" />
                    <GeometryDrawing Brush="#000000"
      Geometry="{StaticResource icon-1}" />
                </DrawingGroup>
            </DrawingImage.Drawing>
        </DrawingImage>
     </Setter.Value>
    </Setter>
    </style>

有人可以建议,有没有办法将路径和形状的组合作为图像源的输入。?

1 个答案:

答案 0 :(得分:0)

不要使用图像控件,而是使用ContentPresenter 然后将您的资源放入视图/画布,它可以正常工作

<Style x:Key="MetroButton" TargetType="Button">
    <Setter Property="MinHeight" Value="30"/>
    <Setter Property="Visibility" Value="Visible"/>
    <Setter Property="Foreground" Value="{DynamicResource Foreground}"/>
    <Setter Property="Background" Value="{DynamicResource BackgroundNormal}"/>
    <Setter Property="BorderBrush" Value="{DynamicResource BorderBrushNormal}"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <Border SnapsToDevicePixels="True"
                        BorderThickness="1"
                        Margin="{TemplateBinding Padding}" 
                        BorderBrush="{TemplateBinding BorderBrush}" 
                        Background="{TemplateBinding Background}">
                    <Grid SnapsToDevicePixels="True">
                        <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
                    </Grid>
                </Border>

和资源

<Canvas x:Shared="False" Width="48" Height="48" Clip="F1 M 0,0L 48,0L 48,48L 0,48L 0,0" x:Key="appbar_add">
    <Path Width="24" Height="24" Canvas.Left="12" Canvas.Top="12" Stretch="Fill" Fill="Black" Data="F1 M 22,12L 26,12L 26,22L 36,22L 36,26L 26,26L 26,36L 22,36L 22,26L 12,26L 12,22L 22,22L 22,12 Z " />
</Canvas>