【问题标题】:How create shared style for path control in UWP?如何在 UWP 中为路径控制创建共享样式?
【发布时间】:2020-01-23 15:03:45
【问题描述】:

我有下一个 XAML 代码:

 <Grid>
    <Grid.Resources>
        <ResourceDictionary Source="Styles.xaml"/>
    </Grid.Resources>
    <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
        <Path Style="{StaticResource UserIcon}"/>
        <Path Style="{StaticResource UserIcon}"/>
    </StackPanel>
</Grid>

Styles.xaml 中的样式:

<Style x:Key="UserIcon"
       TargetType="Path">
    <Style.Setters>
        <Setter Property="Data" Value="M0 58l0 13 70 0 0 -12c-39,-33 -70,-1 -70,-1zm53 -40c0,-10 -8,-18 -18,-18 -9,0 -17,8 -17,18 0,9 8,17 17,17 10,0 18,-8 18,-17z" />
        <Setter Property="Fill" Value="Black"/>
    </Style.Setters>
</Style>

问题:样式只使用了一次。第二个路径控件没有来自样式的数据,并且在设计器(和执行)中看起来像this

【问题讨论】:

    标签: c# xaml uwp .net-standard


    【解决方案1】:

    我怀疑这里的问题是Data 不是依赖属性(更新:事实上这不是真的,所以它似乎是一个错误)。我发现重用路径的唯一方法是将几何数据仅作为简单字符串重用:

    <Page.Resources>
        <x:String x:Key="UserIconGeometry">M0 58l0 13 70 0 0 -12c-39,-33 -70,-1 -70,-1zm53 -40c0,-10 -8,-18 -18,-18 -9,0 -17,8 -17,18 0,9 8,17 17,17 10,0 18,-8 18,-17z</x:String>
    </Page.Resources>
    <StackPanel>
        <Path Data="{StaticResource UserIconGeometry}" Fill="Red" />
        <Path Data="{StaticResource UserIconGeometry}" Fill="Red" />
    </StackPanel>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-24
      • 1970-01-01
      • 2012-08-08
      • 2018-06-10
      • 1970-01-01
      • 1970-01-01
      • 2014-08-16
      • 1970-01-01
      相关资源
      最近更新 更多