WPF-资源-逻辑资源

逻辑资源是一些存储在元素的Resources属性中的.NET对象。也可以叫做“XAML资源”。

由于FrameworkElement和FrameworkContentElement基类都有这个Resources属性(System.Windows.ResourceDictionary)。

举例:注意控件的Background和BorderBrush属性。

<Window x:Class="WpfApp1.ResourceTest"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp1"
        Background="Yellow"
        mc:Ignorable="d"
        Title="ResourceTest" Height="450" Width="800">
    <StackPanel>
        <Button ToolTip="ContentResource" Background="Yellow" BorderBrush="Red" Margin="5">
            <Image Height="20" Width="20" Source="/Resource/Content.png"></Image>
        </Button>
        <Button ToolTip="Resource" Background="Yellow" BorderBrush="Red" Margin="5">
            <Image Height="20" Width="20" Source="/Resource/new.jpg"></Image>
        </Button>
        <Button ToolTip="Resource" Background="Yellow" BorderBrush="Red" Margin="5">
            <Image Height="20" Width="20" Source="/CommandTest;Component/Content.png"></Image>
        </Button>
    </StackPanel>
</Window>

使用逻辑资源重新定义控件的样式。资源定义在Resources属性中。

<Window x:Class="WpfApp1.ResourceTest"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp1"
        mc:Ignorable="d"
        Title="ResourceTest" Height="450" Width="800">
    <Window.Resources>
        <SolidColorBrush x:Key="backgroupBrush">Yellow</SolidColorBrush>
        <SolidColorBrush x:Key="borderBrush">Red</SolidColorBrush>
    </Window.Resources>
    <Window.Background>
        <StaticResource ResourceKey="backgroupBrush"></StaticResource>
    </Window.Background>
    <StackPanel>
        <Button ToolTip="ContentResource" Background="{StaticResource backgroupBrush}" BorderBrush="{StaticResource borderBrush}" Margin="5">
            <Image Height="20" Width="20" Source="/Resource/Content.png"></Image>
        </Button>
        <Button ToolTip="Resource" Background="{StaticResource backgroupBrush}" BorderBrush="{StaticResource borderBrush}" Margin="5">
            <Image Height="20" Width="20" Source="/Resource/new.jpg"></Image>
        </Button>
        <Button ToolTip="Resource" Background="{StaticResource backgroupBrush}" BorderBrush="{StaticResource borderBrush}" Margin="5">
            <Image Height="20" Width="20" Source="/CommandTest;Component/Content.png"></Image>
        </Button>
    </StackPanel>
</Window>

 

资源查找

  StaticResource标记扩展只有一个参数,ResourceKey表示资源字典中项的键值。

  标记扩展类实现了遍历逻辑树的能力。首先检查当前元素的的资源字典,如果资源没有找到;他会检查父元素、父父元素,不断向上查找,直到到达根元素为止。如果在程序层面资源没有找到,他会检查系统集合。如果资源最终没有找到,会抛出一个InvalidOperationException异常。

静态资源和动态资源

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

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

区别分析

  静态资源和动态资源的主要区别。资源的更新会反映在使用了动态资源的元素上。

  性能区别,动态资源需要跟踪变化,占用更多的资源。

  加载时间,静态资源必在Window或Page加载之后加载;动态资源在实际使用时加载。

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

  使用区别,静态资源在引用前必须声明,动态资源不用。

资源整合

  资源可以分别在多个文件(ResourceDictionary)中定义。在使用时引用多个文件整合资源;如果有同名资源,按引用顺序保留最后一个引用。

  

<Window.Resources>        
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Resource/Dictionary1.xaml"></ResourceDictionary>
            </ResourceDictionary.MergedDictionaries>
            <SolidColorBrush x:Key="backgroupBrush">Yellow</SolidColorBrush>
            <SolidColorBrush x:Key="borderBrush">Red</SolidColorBrush>
        </ResourceDictionary>
    </Window.Resources>

程序代码中定义和应用资源

添加资源

window.Resources.Add("backgroundBrush",new SolidColorBrush("Yellow"));

使用资源

  设置静态资源,button.Backgroup = (Brush)button.FindResource("backgroupBrush");

  设置动态资源,button.SetResourceReference(Button.BackgroupProperty,"backgroupBrush");

跨程序集访问逻辑资源

  逻辑资源定义:

<SolidColorBrush x:Key="{ComponentResourceKey TypeInTargetAssembly={x:Type local:MyClass}, ResourceId=MyClassBrush}"></SolidColorBrush>

  使用定义

<Button ToolTip="Resource" Background="{DynamicResource {ComponentResourceKey TypeInTargetAssembly=otherAssembly:MyClass,ResourceId=MyClassBrush}}" 
        </Button>

 

posted @ 2020-05-11 14:43  snake1118  阅读(145)  评论(0编辑  收藏  举报