(转载)资源字典(Pro WPF 学习)
原地址:http://www.cnblogs.com/yxhq/archive/2012/07/09/2582508.html
1、创建资源字典
下面是一个资源字典(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也是非常完美的,这样它会被嵌入到程序集中,但是不会被编译。当然,在运行时解析它的速度要慢一些。
2、使用资源字典
为了使用资源字典,需要将其合并到某些位置的资源集合中。通常合并到应用程序的资源集合中(App.xaml)。

<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>

3、在程序集之间共享资源
将资源字典编译到一个单独的类库程序集中。资源字典必须放到generic.xaml文件中,且该文件必须在Themes文件夹中。
在解决方案右键——添加——新建项目——Visual C#——Windows——WPF自定义控件库,可自动生成需要的文件。


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: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>
(转载)资源字典(Pro WPF 学习)的更多相关文章
- WPF学习之路初识
WPF学习之路初识 WPF 介绍 .NET Framework 4 .NET Framework 3.5 .NET Framework 3.0 Windows Presentation Found ...
- 【WPF学习】第三十五章 资源字典
如果希望在多个项目之间共享资源,可创建资源字典.资源字典只是XAML文档,除了存储希望使用的资源外,不做其他任何事情. 一.创建资源字典 下面是一个资源字典示例,它包含一个资源: <Resour ...
- WPF学习笔记-使用自定义资源字典(style)文件
1.添加资源字典文件style.xmal 2.在资源字典中添加自定义style等 <ResourceDictionary xmlns="http://schemas.microsoft ...
- [WPF 学习] 3.用户控件库使用资源字典的困惑
项目需要(或者前后端分离的需要),前端我使用了用户控件库,由后端用代码加载和控制. 然而用户控件库没法指定资源字典,于是在用户控件的xaml文件里面手工添加了资源字典 <UserControl. ...
- WPF学习之资源-Resources
WPF学习之资源-Resources WPF通过资源来保存一些可以被重复利用的样式,对象定义以及一些传统的资源如二进制数据,图片等等,而在其支持上也更能体现出这些资源定义的优越性.比如通过Resour ...
- WPF使用资源字典组织资源
转载:http://blog.163.com/wangzhenguo2005@126/blog/static/371405262010111413321728/ 首先在解决方案资源管理器中添加 ...
- [WPF]资源字典——程序集之间的资源共享 简单换皮肤
直接上代码,已便已后自己查况阅,新手也可以看! 1.新建一个资料类和一个WPF工程 2.APP.XAML应该资源字典,注意应Source格式,前面一定要有“/” <ResourceDiction ...
- WPF之资源字典zz
最近在看wpf相关东西,虽然有过两年的wpf方面的开发经验,但是当时开发的时候,许多东西一知半解,至今都是模模糊糊,框架基本是别人搭建,自己也就照着模板写写,现在许多东西慢慢的理解了,回顾以前的若干记 ...
- WPF合并资源字典
1.合并多个外部资源字典成为本地字典 示例代码 <Page.Resources> <ResourceDictionary> <ResourceDictionary.Mer ...
随机推荐
- POJ-3187
Backward Digit Sums Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7634 Accepted: 43 ...
- day1 java基础回顾- 文件路径
绝对路径 以根目录或某盘符开头的路径(或者说完整的路径) 例如: l c:/a.txt (Windows操作系统中) l c:/xxx/a.txt (Windows操作系统中) l /var/x ...
- ubuntu12.04+virtualbox+winxp的关于摄像头无法使用,声音出不来的问题
前天在ubuntu上安装了个virtualbox的虚拟机.以前在windows下面是用的vmware.结果到了ubuntu下面折腾半天用不了,于是就装了个virtualbox,在virtualbox里 ...
- QDUOJ ycb的ACM进阶之路 二进制多重背包
ycb的ACM进阶之路 发布时间: 2017年5月22日 14:30 最后更新: 2017年5月22日 14:31 时间限制: 1000ms 内存限制: 128M 描述 ycb是个天资聪颖 ...
- spring配置与使用
1. 创建基于java的配置. 配置极少量的XML来启用java配置: <?xml version="1.0" encoding="UTF-8"?> ...
- linux mysql 简单记录
mysql 1.linux下启动mysql的命令:mysqladmin start/ect/init.d/mysql start (前面为mysql的安装路径) 2.linux下重启mysql的命令: ...
- 2017-9-13 NOIP模拟赛[xxy]
全排列 (permutation.cpp/c/pas)Description从 n 个不同元素中任取 m(m≤n)个元素,按照一定的顺序排列起来,叫做从 n个不同元素中取出 m 个元素的一个排列.当 ...
- Arrange the Bulls
题目链接 #include <stdio.h> #include <algorithm> #include <string.h> #include <iost ...
- 《SQL 进阶教程》 case:在 UPDATE 语句里进行条件分支
1.对当前工资为30万日元以上的员工,降薪10%:2.对当前工资为25万日元以上且不满28万日元的员工,加薪20% update salaries set salary = case when sal ...
- linux 03 命令 续
linux 03 命令 续 一.vim 两种操作方式:新文件 pyvip@Vip:~/demo/2_3$ vim demo.txt #操作一个新文件 一开始进入的是命令模式,按i进入插入模式,开始编辑 ...