【问题标题】:Can't create multiple instances of Geometry Drawing [duplicate]无法创建几何图形的多个实例 [重复]
【发布时间】:2018-12-10 14:34:37
【问题描述】:

我的应用程序中的图标作为几何图形存储在资源字典中。例如:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Viewbox x:Key="ViewboxIconClose"
         Width="16"
         Height="16">
    <Rectangle Width="16" Height="16">
        <Rectangle.Fill>
            <DrawingBrush>
                <DrawingBrush.Drawing>
                    <DrawingGroup>
                        <DrawingGroup.Children>
                            <GeometryDrawing Brush="#00FFFFFF" Geometry="F1M16,16L0,16 0,0 16,0z" />
                            <GeometryDrawing Brush="#FFFFFFFF" Geometry="F1M9.4141,8L13.9571,12.543 12.5431,13.957 8.0001,9.414 3.4571,13.957 2.0431,12.543 6.5861,8 2.0431,3.457 3.4571,2.043 8.0001,6.586 12.5431,2.043 13.9571,3.457z" />
                        </DrawingGroup.Children>
                    </DrawingGroup>
                </DrawingBrush.Drawing>
            </DrawingBrush>
        </Rectangle.Fill>
    </Rectangle>
</Viewbox>

这个图标是这样使用的:

<Button>
    <StaticResource ResourceKey="ViewboxIconClose" />
</Button>

现在我的问题: 如果我在其他地方使用这个几何图形,它只会在一个地方工作。例如,如果我在菜单中使用此几何图形,按钮上的几何图形将立即消失,我打开菜单。

【问题讨论】:

    标签: wpf xaml


    【解决方案1】:

    您可以使用 x:Shared=false 来解决这个问题,但我可能会使用样式和图像来代替。图像应该比带有画笔和视图框的矩形更有效。

        Title="MainWindow" Height="350" Width="525"
    
        xmlns:PresentationOptions="http://schemas.microsoft.com/winfx/2006/xaml/presentation/options" 
        >
    <Window.Resources>
        <Style x:Key="CloseIcon" TargetType="Image">
            <Setter Property="Stretch" Value="Uniform"/>
            <Setter Property="Source">
                <Setter.Value>
                    <DrawingImage PresentationOptions:Freeze="True">
                        <DrawingImage.Drawing>
                            <DrawingGroup>
                                <GeometryDrawing Brush="#00FFFFFF" Geometry="F1M16,16L0,16 0,0 16,0z" />
                                <GeometryDrawing Brush="#FFFFFFFF" Geometry="F1M9.4141,8L13.9571,12.543 12.5431,13.957 8.0001,9.414 3.4571,13.957 2.0431,12.543 6.5861,8 2.0431,3.457 3.4571,2.043 8.0001,6.586 12.5431,2.043 13.9571,3.457z" />
                            </DrawingGroup>
                        </DrawingImage.Drawing>
                    </DrawingImage>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>
    <Grid>
        <StackPanel>
            <Grid Height="100" Width="100" Background="Red">
                <Image  Style="{StaticResource CloseIcon}"/>
            </Grid>
            <Grid Height="30" Width="30" Background="Blue">
                <Image  Style="{StaticResource CloseIcon}"/>
            </Grid>
        </StackPanel>
    </Grid>
    

    根据这一特定要求,您可以只使用一个几何图形作为路径数据。几何图形不是视觉效果。

    【讨论】:

      猜你喜欢
      • 2019-12-31
      • 1970-01-01
      • 2021-11-01
      • 2022-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多