Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在XAML中重用路径对象_C#_.net_Wpf_Xaml - Fatal编程技术网

C# 在XAML中重用路径对象

C# 在XAML中重用路径对象,c#,.net,wpf,xaml,C#,.net,Wpf,Xaml,我有一个路径(星号): 现在,我想多次复制此路径(仅参考“NiceStar”)。我可以在纯XAML中执行此操作吗 通过这样做,我可以使用它一次: <Decorator Child="{StaticResource star}" /> 但是,我不能重复这一行。我的编译器说: 指定的元素已经是另一个元素的逻辑子元素。先把它断开 当然,只需定义路径的样式,然后就可以将其作为静态资源重用: <Page xmlns="http://schemas.microsoft.c

我有一个路径(星号):


现在,我想多次复制此路径(仅参考“NiceStar”)。我可以在纯XAML中执行此操作吗

通过这样做,我可以使用它一次:

<Decorator Child="{StaticResource star}" />

但是,我不能重复这一行。我的编译器说:

指定的元素已经是另一个元素的逻辑子元素。先把它断开


当然,只需定义路径的样式,然后就可以将其作为静态资源重用:

<Page
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Page.Resources>
    <Style x:Key="StarStyle" TargetType="Path">
    <Setter>
        <Setter.Property>Fill</Setter.Property>
        <Setter.Value>                
             <RadialGradientBrush MappingMode="Absolute"
              GradientOrigin="390.395508,448.130371" Center="390.395508,448.130371"
              RadiusX="113.034821" RadiusY="113.034821">
             <RadialGradientBrush.Transform>
                 <MatrixTransform Matrix="1,0,-0,-1,-263.816895,569.592773" />
             </RadialGradientBrush.Transform>
             <GradientStop Offset="0" Color="#ff00ff00"/>
             <GradientStop Offset="1" Color="#ff006736"/>
             </RadialGradientBrush>
         </Setter.Value> 
    </Setter>
    <Setter Property="StrokeThickness" Value="10" />
    <Setter Property="Stroke" Value="#ff000000" />
    <Setter Property="StrokeMiterLimit" Value="1" />
    <Setter Property="Data" Value="F1 M 126.578613,11.297852 L 162.373535,83.825684 L 242.412598,95.456055 L 184.495605,151.911133 L 198.167480,231.626953 L 126.578613,193.990234 L 54.988770,231.626953 L 68.661621,151.911133 L 10.744629,95.456055 L 90.783691,83.825684 L 126.578613,11.297852 Z"/>
    </Style>
</Page.Resources>
<StackPanel>  
    <Path Style="{StaticResource StarStyle}" />
    <Path Style="{StaticResource StarStyle}" />
</StackPanel>
</Page>
<ResourceDictionary>
    <Geometry x:Key="StarPathData">F1 M 126.578613,11.297852 L 162.373535,83.825684 L 242.412598,95.456055 L 184.495605,151.911133 L 198.167480,231.626953 L 126.578613,193.990234 L 54.988770,231.626953 L 68.661621,151.911133 L 10.744629,95.456055 L 90.783691,83.825684 L 126.578613,11.297852 Z</Geometry>
</ResourceDictionary>

填满
创建一个样式

<Style x:Key="NiceStarPath" TargetType="{x:Type Path}">
    <Setter Property="StrokeThickness" Value="10"/>
    <Setter Property="Stroke" Value="#FF000000"/>
    <Setter Property="StrokeMiterLimit" Value="1"/>
    <Setter Property="Data" Value="F1 M 126.578613,11.297852 L 162.373535,83.825684 L 242.412598,95.456055 L 184.495605,151.911133 L 198.167480,231.626953 L 126.578613,193.990234 L 54.988770,231.626953 L 68.661621,151.911133 L 10.744629,95.456055 L 90.783691,83.825684 L 126.578613,11.297852 Z"/>
    <Setter Property="Fill">
        <Setter.Value>
            <RadialGradientBrush MappingMode="Absolute" GradientOrigin="390.395508,448.130371" Center="390.395508,448.130371" RadiusX="113.034821" RadiusY="113.034821">
                <RadialGradientBrush.Transform>
                    <MatrixTransform Matrix="1,0,-0,-1,-263.816895,569.592773" />
                </RadialGradientBrush.Transform>
                <GradientStop Offset="0" Color="#ff00ff00"/>
                <GradientStop Offset="1" Color="#ff006736"/>
            </RadialGradientBrush>
        </Setter.Value>
    </Setter>
</Style>


在相关说明中(虽然可能没有直接回答您的问题),您还可以将框架元素声明为资源,给它一个键,并且只要添加
x:Shared=“False”
您就可以在代码中一次又一次地访问资源

下面是一个伪编码示例:

<Window ....>
   <Window.Resources>
      <Ellipse x:Key="ReusableEllipse" x:Shared="False" ...>
         <Ellipse.Fill>
            <!--STUFF-->
         </Ellipse.Fill>
      </Ellipse>
   </Window.Resources>
   <Canvas x:Name="drawCanvas" Background="White"/>
</Window>

我会把这条路变成一条小路。这在blend中很容易做到,只需选择路径,工具>制作笔刷资源>制作DrawingBrush资源。然后,您就可以在资源中使用画笔,随时可以重用。我希望它的性能会非常好,因为画笔是非交互式的,并且是可重用的

以下是XAML:

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Width="640" Height="480">
    <Window.Resources>
        <DrawingBrush x:Key="NiceStarBrush" Viewbox="0,0,250,240" ViewboxUnits="Absolute">
            <DrawingBrush.Drawing>
                <GeometryDrawing Geometry="F1M126.578613,11.297852L162.373535,83.825684 242.412598,95.456055 184.495605,151.911133 198.16748,231.626953 126.578613,193.990234 54.98877,231.626953 68.661621,151.911133 10.744629,95.456055 90.783691,83.825684 126.578613,11.297852z">
                    <GeometryDrawing.Brush>
                        <RadialGradientBrush MappingMode="Absolute" Center="390.395508,448.130371" GradientOrigin="390.395508,448.130371" RadiusX="113.034821" RadiusY="113.034821">
                            <RadialGradientBrush.Transform>
                                <MatrixTransform Matrix="1,0,0,-1,-263.816895,569.592773"/>
                            </RadialGradientBrush.Transform>
                            <GradientStop Color="Lime" Offset="0"/>
                            <GradientStop Color="#FF006736" Offset="1"/>
                        </RadialGradientBrush>
                    </GeometryDrawing.Brush>
                    <GeometryDrawing.Pen>
                        <Pen Brush="Black" DashCap="Flat" EndLineCap="Flat" LineJoin="Miter" MiterLimit="1" StartLineCap="Flat" Thickness="10">
                            <Pen.DashStyle>
                                <DashStyle/>
                            </Pen.DashStyle>
                        </Pen>
                    </GeometryDrawing.Pen>
                </GeometryDrawing>
            </DrawingBrush.Drawing>
        </DrawingBrush>
    </Window.Resources>
    <Grid x:Name="LayoutRoot">
        <Rectangle Margin="181,115,0,0" Fill="{DynamicResource NiceStarBrush}" HorizontalAlignment="Left" VerticalAlignment="Top" Width="50" Height="46" />
    </Grid>
</Window>


另一个选项是使用

将路径包装到imagesource。您可以将样式与控件模板一起使用

<Style TargetType="Control" x:Key="FolderIcon">
    <Setter Property="IsTabStop" Value="False"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate>
                <Path Data="M -1,0 h 5 M 0,3 h 10 v 5 h -10 Z" StrokeThickness="2" Stroke="White" Fill="White" />
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

然后使用它:

<Button>
    <Control Style="{StaticResource FolderIcon}"/>
</Button>

我在单独的XAML文档中定义了一个路径,然后将其重新用作ContentControl的内容

在资源文件或资源字典中:

<Path  x:Key="SearchIcon"
        Width="30" Height="30" Margin="3" Fill="Blue"
        Data="Path data here"            
        Stretch="Fill" />

在WPF控件中:

<ContentControl Content="{StaticResource SearchIcon}"></ContentControl>

如果要多次重用路径数据(可能在多个样式之间),可以定义几何图形资源:

<Page
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Page.Resources>
    <Style x:Key="StarStyle" TargetType="Path">
    <Setter>
        <Setter.Property>Fill</Setter.Property>
        <Setter.Value>                
             <RadialGradientBrush MappingMode="Absolute"
              GradientOrigin="390.395508,448.130371" Center="390.395508,448.130371"
              RadiusX="113.034821" RadiusY="113.034821">
             <RadialGradientBrush.Transform>
                 <MatrixTransform Matrix="1,0,-0,-1,-263.816895,569.592773" />
             </RadialGradientBrush.Transform>
             <GradientStop Offset="0" Color="#ff00ff00"/>
             <GradientStop Offset="1" Color="#ff006736"/>
             </RadialGradientBrush>
         </Setter.Value> 
    </Setter>
    <Setter Property="StrokeThickness" Value="10" />
    <Setter Property="Stroke" Value="#ff000000" />
    <Setter Property="StrokeMiterLimit" Value="1" />
    <Setter Property="Data" Value="F1 M 126.578613,11.297852 L 162.373535,83.825684 L 242.412598,95.456055 L 184.495605,151.911133 L 198.167480,231.626953 L 126.578613,193.990234 L 54.988770,231.626953 L 68.661621,151.911133 L 10.744629,95.456055 L 90.783691,83.825684 L 126.578613,11.297852 Z"/>
    </Style>
</Page.Resources>
<StackPanel>  
    <Path Style="{StaticResource StarStyle}" />
    <Path Style="{StaticResource StarStyle}" />
</StackPanel>
</Page>
<ResourceDictionary>
    <Geometry x:Key="StarPathData">F1 M 126.578613,11.297852 L 162.373535,83.825684 L 242.412598,95.456055 L 184.495605,151.911133 L 198.167480,231.626953 L 126.578613,193.990234 L 54.988770,231.626953 L 68.661621,151.911133 L 10.744629,95.456055 L 90.783691,83.825684 L 126.578613,11.297852 Z</Geometry>
</ResourceDictionary>

F1 M 126.578613,11.297852 L 162.373535,83.825684 L 242.412598,95.45605L 184.495605151.911133 L 198.167480231.626953 L 126.578613193.990234 L 54.988770231.626953 L 68.661621151.911133 L 10.744629,95.456055 L 90.783691,83.825684 L 126.578613,11.297852 Z
然后直接使用它:

<Path Data="{StaticResource StarPathData}">

或以某种方式:

<Style x:Key="NiceStarPath" TargetType="{x:Type Path}">
    <Setter Property="Data" Value="{StaticResource StarPathData}"/>
    ...
</Style>

...
<Style x:Key="NiceStarPath" TargetType="{x:Type Path}">
    <Setter Property="Data" Value="{StaticResource StarPathData}"/>
    ...
</Style>