(转载)资源字典(Pro WPF 学习)

原地址:http://www.cnblogs.com/yxhq/archive/2012/07/09/2582508.htmlhtml

一、建立资源字典post

下面是一个资源字典(AppBrushes.xaml),包含一个资源:性能

复制代码
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:ResourceLibrary">
  
    <ImageBrush
      x:Key="TileBrush" TileMode="Tile" ViewportUnits="Absolute" Viewport="0 0 32 32"
      ImageSource="ResourceLibrary;component/sadface.jpg" Opacity="0.3">
    </ImageBrush>
</ResourceDictionary>
复制代码

为应用程序添加资源字典时候,须要确保将Build Action 设置为Page。这样能够保证为了获得最佳性能将资源字典编译为BAML。不过,将资源字典的Build Action 设置为Resource也是很是完美的,这样它会被嵌入到程序集中,可是不会被编译。固然,在运行时解析它的速度要慢一些。ui

二、使用资源字典spa

为了使用资源字典,须要将其合并到某些位置的资源集合中。一般合并到应用程序的资源集合中(App.xaml)。code

复制代码
<Application x:Class="Resources.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
    <ResourceDictionary>
      <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="AppBrushes.xaml"/>
        <ResourceDictionary Source="WizardBrushes.xaml"/>
      </ResourceDictionary.MergedDictionaries>
此处能够添加本身的资源
<ImageBrush x:Key="GraphicalBrush1" ... ></ImageBrush> </ResourceDictionary> </Application.Resources> </Application>
复制代码

三、在程序集之间共享资源component

  将资源字典编译到一个单独的类库程序集中。资源字典必须放到generic.xaml文件中,且该文件必须在Themes文件夹中。xml

  在解决方案右键——添加——新建项目——Visual C#——Windows——WPF自定义控件库,可自动生成须要的文件。htm

generic.xaml文件blog

复制代码
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:ResourceLibrary">
 
    <ImageBrush
      x:Key="{ComponentResourceKey TypeInTargetAssembly={x:Type local:CustomResources}, ResourceId=SadTileBrush}"
      TileMode="Tile"
      ViewportUnits="Absolute" Viewport="0 0 32 32"
      ImageSource="ResourceLibrary;component/sadface.jpg" Opacity="0.3">      
    </ImageBrush>
</ResourceDictionary>
复制代码

  ComponentResourceKey 标记扩展
    为从外部程序集加载的资源定义和引用键。 这使得资源查找功能能够在程序集内指定目标类型,而不是在程序集内或类上指定显式的资源字典。

  XAML 特性用法(设置键,精简版)

<object x:Key="{ComponentResourceKey {x:Type targetTypeName}, targetID}" .../>

  XAML 特性用法(设置键,详细版)

<object x:Key="{ComponentResourceKey TypeInTargetAssembly={x:Type targetTypeName}, ResourceID=targetID}" .../>

  XAML 特性用法(请求资源,精简版)

<object property="{DynamicResource {ComponentResourceKey {x:Type targetTypeName}, targetID}}" .../>

  XAML 特性用法(请求资源,详细版)

<object property="{DynamicResource {ComponentResourceKey TypeInTargetAssembly={x:Type targetTypeName}, ResourceID=targetID}}" .../>

  XAML 值

    targetTypeName 在资源程序集内定义的common language runtime (CLR) 公共类型的名称。
    targetID 资源的键。 在查找资源时,targetID 将与资源的 x:Key 指令相似。

使用资源字典

添加引用

复制代码
<Window x:Class="Resources.ResourceFromLibrary"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:res="clr-namespace:ResourceLibrary;assembly=ResourceLibrary"
    Title="ResourceFromLibrary" Height="300" Width="300"    
    >
  <StackPanel Margin="5">
    <Button Background="{DynamicResource {ComponentResourceKey TypeInTargetAssembly={x:Type res:CustomResources}, ResourceId=SadTileBrush}}"
            Padding="5" Margin="5"
            FontWeight="Bold" FontSize="14">
      A Resource From ResourceLibrary</Button>
  </StackPanel>
</Window>
复制代码

  在使用ComponentResourceKey时,必须使用动态资源,不能用静态。

更加容易使用的作法

  能够定义一个静态属性,让他返回须要使用的正确的ComponentResourceKey。一般在组件的类中定义该属性。

复制代码
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;

namespace ResourceLibrary
{
    public class CustomResources
    {
        public static ComponentResourceKey SadTileBrush
        {
            get { return new ComponentResourceKey(typeof(CustomResources), "SadTileBrush"); }
        }
    }
}
复制代码

  如今能够使用 Static 标记扩展访问该属性并应用资源了,而不须要使用很长的ComponentResourceKey

<Button Background="{DynamicResource {x:Static res:CustomResources.SadTileBrush}}"
        Padding="5" Margin="5" FontWeight="Bold" FontSize="14">
      A Resource From ResourceLibrary
</Button>
相关文章
相关标签/搜索