(20)WPF 资源

一、资源基础  

资源的查找层次

每个元素的资源--》应用程序资源--》系统资源

1.元素级别的资源

        <Button  Width="200" Height="50" >
            <Button.Resources>
                <!--应用程序的根目录下-->
                <ImageBrush x:Key="brush" ImageSource="1.jpg"></ImageBrush>
            </Button.Resources>
            <Button.Background>
                <StaticResource ResourceKey="brush"/>
            </Button.Background>
        </Button>

2.应用程序级别的资源

单个界面下

    <Window.Resources>
        <ImageBrush x:Key="brush" ImageSource="1.jpg"></ImageBrush>
    </Window.Resources>
    <StackPanel>
        <Button  Background="{StaticResource brush}" Width="200" Height="50" ></Button>
    </StackPanel>

3.系统级别的资源

整套应用程序都可以使用

文件定义在app.xaml下的<Application.Resources> 元素中

    <Application.Resources>
        <ImageBrush x:Key="brush" ImageSource="1.jpg"></ImageBrush>
    </Application.Resources>

4.静态资源

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

<Button  Background="{StaticResource brush}" Width="200" Height="50" ></Button>

5.动态资源

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

<Button  Background="{DynamicResource brush}" Width="200" Height="50" ></Button>

6.代码访问资源 

二、资源字典

1.创建字典

 单独创建一个xaml文件

Dictionary1.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:WpfApp1">
    <ImageBrush x:Key="brush" ImageSource="1.jpg"></ImageBrush>
</ResourceDictionary>

2.使用字典

在app.xaml文件中引入

    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Dictionary1.xaml" />
                <ResourceDictionary Source="Dictionary2.xaml" />
            </ResourceDictionary.MergedDictionaries>
            <ImageBrush x:Key="brush" ImageSource="1.jpg"></ImageBrush>
        </ResourceDictionary>
    </Application.Resources>

3.程序集

两种方法引入程序集资源字典的方式

创建WPF自定义控件库,名为WpfCustomControlLibrary1

把1.jpg图片拷贝到根目录下

 主程序引入 WpfCustomControlLibrary1类库

 

(1) 使用代码

  •  创建一个资源词典Dictionary1.xaml 文件
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:WpfApp1">
    <ImageBrush x:Key="brush" ImageSource="1.jpg"></ImageBrush>
</ResourceDictionary>
  • 应用程序使用代码
            ResourceDictionary resourceDictionary = new ResourceDictionary();
            resourceDictionary.Source = new Uri("WpfCustomControlLibrary1;component/Dictionary1.xaml",UriKind.Relative);
            btn1.Background=(Brush)resourceDictionary["brush"];
  •  控件名
<Button  x:Name="btn1" Width="200" Height="50" ></Button>

运行

(2)不使用代码的方式

  • 创建一个类,类代码如下(公用空类)
    public class CustomResources
    {
    }
  • 编辑Generic.xaml
<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfCustomControlLibrary1">
    <ImageBrush x:Key="{ComponentResourceKey TypeInTargetAssembly={x:Type local:CustomResources},ResourceId=brush}" ImageSource="WpfCustomControlLibrary1;component/1.jpg"></ImageBrush>
</ResourceDictionary>

WpfCustomControlLibrary1设置完成

在需要用的窗体下引入,MainWindow.xaml

xmlns:res="clr-namespace:WpfCustomControlLibrary1;assembly=WpfCustomControlLibrary1"

 按钮使用此资源

<Button  Background="{DynamicResource {ComponentResourceKey TypeInTargetAssembly={x:Type res:CustomResources},ResourceId=brush}}" x:Name="btn1" Width="200" Height="50" ></Button>

运行,同样的结果

原文地址:https://www.cnblogs.com/buchizaodian/p/12172078.html