(六)WPF - 资源和样式行为(1)

一、资源

1、资源集合

每个元素都有一个 Resources 属性,该属性存储了一个资源字典集合(它是 Resource Dictionary 类的实例)。资源集合可以包含任意类型的对象,并根据字符串编写索引。

为了使用 XML 标记中的资源,需要一种引用资源的方法。这是通过标记扩展完成的。
有两个标记扩展可供使用:一个用于动态资源,另一个用于静态资源。

静态资源: 在第一次创建窗口时一次性地设置完毕。
动态资源: 如果发生了改变,则会重新应用资源。

2、资源的层次

每个元素都有自己的资源集合,并且为了找到期望的资源, WPF 在元素树中进行递归搜索。

使用静态资源时,必须总是在引用资源之前在标记中定义资源。

<Window.Resources>
<ImageBrush x:Key="TileBrush" TileMode="Tile"
			ViewportUnits="Absolute" Viewport" 0 0 32 32"
			ImageSource = "happyface.jpg" opacity="0.3"></ImageBrush>
</Window.Resources>
....

3、访问二进制资源

二进制资源如图片等信息。

<Image Height="21" Source="test.png"

System.Windows.Controls.Image 是一种很方便的访问每一个二进制图像的控件。
Image 类有一个 Source 属性,类型是 System.Windows.Media.ImageSource。

如果把资源变为松散文件,不想把它加入到项目中来。
<Image Height="21" Source="c:\User\Adam\Doucments\test.png"/>
从部署位置访问资源
<Image Height="21" Source="pack://application:,,,/Resources/Image/test.png"/>
<Image Height="21" Source="pack://siteOfOrigin:,,,/Resources/Image/test.png"

4、静态资源和动态资源

WPF 提供了两种访问逻辑资源的方式:

  • 一种静态的,由 StaticResource 实现,这种资源仅会被应用一次(第一次需要资源时加载)。
  • 一种是动态资源,由 DynamicResource 实现,这种资源每次更改时都会被重新应用。

两者区别:

  • 静态资源只从资源集合中获取对象一次。

  • 动态资源在每次需要对象时都会重新从资源集合中查找对象。

  • 动态资源需要占用更多的资源。

  • 使用动态资源可以改善加载时间,对静态资源的引用总是发生在 Window 或 Page 加载之后,而对动态资源的引用要到实际使用时才会生效。

  • 动态资源只能用于设置依赖属性值,而静态资源可以在任何地方使用。

  • 静态资源必须在 XMAL 文件中声明之后才可以使用,而动态资源没有限制。

第一次加载时的性能:

  • 动态资源在第一次使用它们时加载,而静态资源总是在创建窗口时加载。(因此,动态资源还被用于提高第一次加载时的性能)

明显的例子:
当创建依赖于 Windows 设置(例如系统颜色)的资源时。对于这种情况,如果希望能够相应当前颜色方案的任何改变,就需要使用动态资源。否则,如果使用静态资源,将仍然使用原来的颜色方案,直到用户重新启动应用程序为止。

使用动态属性时:

  • 资源具有依赖于系统设置的属性(如当前 Windows 操作系统的颜色或字体)。
  • 计划通过编程替换资源对象(例如,实现几类动态皮肤特性。)

5、非共享资源

当在多个地方使用某种资源时,使用的是同一个对象实例。这种行为——被称为共享。
而希望解释器在每次使用时创建单独的对象实例,就要关闭共享行为。

<ImageBrush x:Key="TileBrush" x Shared="False"...></ImageBrush>

二、样式

<Window.Resources>
<Sytle x:Key="BigFontButtonStyle">
<Setter Property="Control.FontFamily" Value="Times New Roman"/>
<Setter Property="Control.FontSize" Value="18"/>
<Setter Property="Control.FontWeight" Value="Bold"/>
</Style>
</Window.Resources>

表-Sytel 类的属性

属性 说明
Setters 设置属性值以及自动关联事件处理程序的 Setter 对象或 EventSetter 对象的集合
Triggers 继承自 TriggerBase 类并且能够自动改变样式设置的对象的集合。例如,当另一个属性改变时,或者当发生某个事件时,可以修改样式
Resources 希望用于样式的资源集合。例如,可能需要使用一个对象设置多个属性。这时,作为资源创建对象,然后再在 Setter 对象中使用该资源,这样会更搞笑(而不是使用嵌套的标签作为每个 Setter 对象的一部分创建对象)
BasedOn 通过该属性可以创建继承自(并且可以有选择地进行重写)其他样式设置的更复杂样式
TargetType 该属性标识应用样式的元素的类型。通过该属性可以创建只影响特定类型元素的设置器,并且还可以创建能够为恰当的元素类型自动起作用的设置器

1、Setter设置属性

    <Window.Resources>
        <Style x:Key="BigFontButtonStyle" TargetType="Button">
            <Setter Property="FontFamily" Value="Times New Roman"/>
            <Setter Property="FontSize" Value="24"/>
            <Setter Property="FontWeight" Value="Bold"/>
            <Setter Property="Foreground" Value="Blue"/>
        </Style>
    </Window.Resources>
    <StackPanel>
        <Button x:Name="btn" Content="测试按钮" Width="100" Height="100" Style="{ 
        StaticResource BigFontButtonStyle}" >
        </Button>
    </StackPanel>

2、EventSetter 处理事件程序

    <Window.Resources>
        <Style x:Key="CustomTextBlockStyle" TargetType="TextBlock">
            <Setter Property="Foreground" Value="Blue"/>
            <EventSetter Event="TextBlock.MouseEnter"
                             Handler="TextBlock_MouseEnter"/>

            <EventSetter Event="TextBlock.MouseLeave"
                             Handler="TextBlock_MouseLeave"/>
        </Style>
    </Window.Resources>
    <StackPanel>
        <TextBlock x:Name="tbTest" Text="测试" FontSize="30"  Style="{ 
        StaticResource CustomTextBlockStyle}" />
    </StackPanel>

cs 代码:

 private void TextBlock_MouseEnter(object sender,MouseEventArgs e)
        {
            tbTest.Foreground = new SolidColorBrush(Colors.Wheat);
        }

        private void TextBlock_MouseLeave(object sender, MouseEventArgs e)
        {
            tbTest.Foreground = new SolidColorBrush(Colors.Blue);
        }

3、BasedOn 继承样式

    <Window.Resources>
        <Style x:Key="BasedTextBlockStyle" TargetType="TextBlock">
            <Setter Property="Foreground" Value="Blue"/>
            <Setter Property="FontWeight" Value="Bold"/>
            <Setter Property="FontSize" Value="30"/>
        </Style>

        <Style x:Key="CustomTextBlockStyle" TargetType="TextBlock" BasedOn="{StaticResource BasedTextBlockStyle}">
            <Setter Property="Foreground" Value="Black"/>
        </Style>

    </Window.Resources>
    <StackPanel>
        <TextBlock x:Name="tbTest" Text="测试" FontSize="30"  Style="{ 
        StaticResource CustomTextBlockStyle}" />
    </StackPanel>

4、Triggers

    <Window.Resources>
        <Style x:Key="CustomBtnStyle" TargetType="{x:Type Button}">
            <Setter Property="Background" Value="#273c62"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Border x:Name="border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="true">
                            <ContentPresenter x:Name="contentPresenter" Focusable="False" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="true">
                                <Setter Property="Background" TargetName="border" Value="#660000ff"/>
                            </Trigger>
                            <Trigger Property="IsPressed" Value="true">
                                <Setter Property="Background" TargetName="border" Value="#55ff0000"/>
                            </Trigger>
                            <Trigger Property="IsEnabled" Value="false">
                                <Setter Property="Background" TargetName="border" Value="#33000000"/>
                                <Setter Property="Opacity" TargetName="contentPresenter" Value="0.5"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

    </Window.Resources>
    <StackPanel>
        <Button x:Name="btnTest" Content="测试" FontSize="30"  Style="{ 
        StaticResource CustomBtnStyle}" />
    </StackPanel>

猜你喜欢

转载自blog.csdn.net/chen1083376511/article/details/131353730