何时使用基于其他样式的样式变得过度?

问题描述 投票:-1回答:2

与代码的DRY并行,我不喜欢在XAML中具有相同的属性赋值。我已经看过代码示例,其中可以将相当多的代码合并为一种样式。一旦完成并清理了更多代码,就可以用另一种基于初始样式的样式来表示,该样式具有一些更具体的编辑,依此类推。有一点,这会导致风格“聪明”。其中一种风格的变化会导致所有依赖它的人产生连锁效应。使用Style ... BasedOn={...}时是否需要记住经验法则或一般准则?

Head First C#的一个例子,使用WPF version。在页754,他们有如下所示的示例。由于这是一个介绍,有几个属性分配可以使用样式进行合并。

    <StackPanel Margin="20">
        <TextBlock Foreground="White" FontFamily="Segoe" FontSize="20px"
                   FontWeight="Bold" Text="{Binding TeamName}" />
        <TextBlock Foreground="White" FontFamily="Segoe" FontSize="16px" 
                   Text="Starting Players" Margin="0,5,0,0"/>
        <ListView Background="Black" Foreground="White"  Margin="0,5,0,0"
                                   ItemTemplate="{StaticResource PlayerItemTemplate}"
                                   ItemsSource="{Binding Starters}" />
        <TextBlock Foreground="White"  FontFamily="Segoe" FontSize="16px"
                   Text="Bench Players" Margin="0,5,0,0"/>
        <ListView Background="Black" Foreground="White" ItemsSource="{Binding Bench}" 
                  ItemTemplate="{StaticResource PlayerItemTemplate}" Margin="0,5,0,0"/>
    </StackPanel>

使用样式重构以删除重复的属性分配。这显示了一个中间步骤,可以进一步巩固Margin财产。

<StackPanel Margin="20">
    <StackPanel.Resources>
        <Style x:Key="whiteForground" TargetType="TextBlock">
            <Setter Property="Foreground" Value="White" />
            <Setter Property="FontFamily" Value="Segoe" />
        </Style>
        <Style TargetType="ListView">
            <Setter Property="Margin" Value="0,5,0,0" />
            <Setter Property="Background" Value="Black" />
            <Setter Property="Foreground" Value="White" />
        </Style>
        <Style x:Key="appliedMargin" TargetType="TextBlock" BasedOn="{StaticResource whiteForground}">
            <Setter Property="Margin" Value="0,5,0,0" />
        </Style>
    </StackPanel.Resources>
    <TextBlock Style="{StaticResource whiteForground}" FontSize="20px"
                FontWeight="Bold" Text="{Binding TeamName}" />
    <TextBlock Style="{StaticResource appliedMargin}" FontSize="16px"
                Text="Starting Players" />
    <ListView ItemTemplate="{StaticResource PlayerItemTemplate}"
                ItemsSource="{Binding Starters}" />

    <TextBlock Style="{StaticResource appliedMargin}" FontSize="16px"
                Text="Bench Players" />
    <ListView ItemsSource="{Binding Bench}"
                ItemTemplate="{StaticResource PlayerItemTemplate}" />
</StackPanel>
wpf
2个回答
0
投票

我试着只坚持一个基于的水平。

所以基本风格,只有一种继承(叶子)风格。

但是,很明显,设置值会覆盖控件中的任何一个,因此使用了叶子。

最多是另一层。

有几个原因。

1)

这是追踪复杂遗产的噩梦 - 你可能已经意识到了。

2)

曾经(并且可能仍然是)资源字典链(rd)的潜在问题。

你想要做的是让你的风格在rd。

这些往往变得非常快。

所以你把它们分成了一些rd。

您希望将一个基于另一个,以便需要了解基本样式。因此,您在叶子中合并基础的一个。

一层合并字典如:

<ResourceDictionary 
...
<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary  Source="pack://application:,,,/Blaa.Validation.UILib;component/Resources/UIlibResources.xaml"/>
</ResourceDictionary.MergedDictionaries>
<Style TargetType="{x:Type ComboBox}"   BasedOn="{StaticResource ErrorStyle}"/>
<Style TargetType="{x:Type DatePicker}" BasedOn="{StaticResource ErrorStyle}"/>

这里的组合框样式基于UILib Resources中的errorstyle。

这很好用。

如果一个rd合并另一个rd又合并另一个rd ......依此类推。

你可能会遇到神秘的问题,这些问题似乎来自合并延迟。

我见过间歇性错误和样式问题。

我认为最好保持链接浅,基本资源字典很小。

这可能已经在框架的最新版本中得到修复,但我没有看到它,并且WPF在很长一段时间内几乎没有变化。

3)

在此之前发现你需要这个级别太容易了,但如果你试着变得太“聪明”,它还取决于其他东西。

如果风格级联或有混合,但它们没有,我们没有,这将是伟大的。

因此,基于这些东西,我们拥有的东西,与网络相比,它有点笨重。


© www.soinside.com 2019 - 2024. All rights reserved.