WPF整理-使用ResourceDictionary管理Logical Resources

“Logical resources may be of various types, such as brushes, geometries, styles, and templates.
Placing all those resources in a single file such as App.xaml hinders maintainability. A better
approach would be to separate resources of different types (or based on some other criteria) to
their own files. Still, they must be referenced somehow from within a common file such as App.
xaml so they are recognized.”

为了增加资源文件的可维护性,我们应该使用ResourceDictionary对资源进行:分类、汇总。

如何实现呢?举个例子

1.新建一个WPF Application,在Application中添加一个New Item,选择ResourceDictionary。

譬如,命名为Brushes.xaml,我们用它来存放一些笔刷。打开,我们添加一个笔刷如下:

Brushes.xaml:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<LinearGradientBrush EndPoint="1,0" x:Key="brush1">
<GradientStop Color="Violet" Offset="0" />
<GradientStop Color="Orange" Offset=".7" />
<GradientStop Color="Brown" Offset="1" />
</LinearGradientBrush>
</ResourceDictionary>

2.在App.xaml中Merge则个Resource。
“Open App.xaml. We need to merge external resource dictionaries into the main
application dictionary.

打开App.xaml,添加如下内容:

<Application x:Class="ManagingLogicalResources.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="Brushes.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>

3.这样我们就可以在页面中正常使用了。

<Window x:Class="ManagingLogicalResources.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Ellipse Fill="{StaticResource brush1}"/>
</Grid>
</Window>

效果如下:

 
 

使用ResourceDictionary管理Logical Resources的更多相关文章

  1. WPF整理-使用ResourceDictionary管理Logical Resources

    “Logical resources may be of various types, such as brushes, geometries, styles, and templates.Placi ...

  2. 学习OpenStack之 (4): Linux 磁盘、分区、挂载、逻辑卷管理 (Logical Volume Manager)

    0. 背景: inux用户安装Linux操作系统时遇到的一个常见的难以决定的问题就是如何正确地评估各分区大小,以分配合适的硬盘空间.普通的磁盘分区管理方式在逻辑分区划分好之后就无法改变其大小,当一个逻 ...

  3. 背水一战 Windows 10 (11) - 资源: CustomResource, ResourceDictionary, 加载外部的 ResourceDictionary 文件

    [源码下载] 背水一战 Windows 10 (11) - 资源: CustomResource, ResourceDictionary, 加载外部的 ResourceDictionary 文件 作者 ...

  4. 资源: CustomResource, ResourceDictionary, 加载外部的 ResourceDictionary 文件

    CustomResource ResourceDictionary 加载外部的 ResourceDictionary 文件 示例1.演示“CustomResource”相关知识点Resource/Cu ...

  5. CentOS 6.7 配置LVM (逻辑卷管理)

    LVM 简介 LVM是逻辑盘卷组管理 (Logical Volume Manager) 的简称. LVM是建立在硬盘和分区之上的一个逻辑层,来提高磁盘分区管理的灵活性,在一定程度上解决普通磁盘分区带来 ...

  6. webmin纯web界面管理linux系统

    关键字: 摘要:从Windows环境的管理转到Linux环境的管理时所面临的挑战之一是,您需要去学习利用新的工具.作为一个管理员,您希望理解操作系统的细节以发挥它的最大功效.但是,当您还处在学习阶段时 ...

  7. WPF学习一:XAML的资源(Resources)结构

    一个初学者,把知识做个积累,如果有不对的地方,还请高手指出,谢谢! 先看一段代码:(下面是以Window WPF进行讲解,如果是Web 的话就把<Window改为<Page 而如果是 Us ...

  8. linux LVM逻辑卷管理

    什么是LVM LVM是逻辑卷管理(Logical Volume Manager)的简称,它是建立在物理存储设备之上的一个抽象层,允许你生成逻辑存储卷,与直接使用物理存储在管理上相比,提供了更好灵活性. ...

  9. WPF整理-使用逻辑资源

    "Traditional application resources consist of binary chunks of data, typically representing thi ...

随机推荐

  1. Cocos2d-x 3.0 编译出错 解决 error: expected &#39;;&#39; at end of member declaration

    近期把项目移植到cocos2d-x 3.0,在整Android编译环境的时候,出现一大堆的编译出错,都是类似"error: expected ';' at end of member dec ...

  2. 使用线程执行堆栈StackTraceElement设计Android日志模块

    假设你想在你的Android自己主动打印程序MainActivity.onCreate(line:37)这样的类名.方法名称(行)登录如何实现? 1.介绍Java线程执行堆栈  Java.lang包中 ...

  3. PDFBox之文档创建

    1.创建一个空的PDF 下面的小例子表示如何使用PDFBox来创建一个新的PDF文档. // 创建一个空的文档 PDDocument document = new PDDocument(); // 创 ...

  4. Linux下使用Photorec恢复误格U盘

    photorec包含在testdisk软件包中, 所以直接通过包管理器直接安装testdisk安装即可. 使用root权限来运行软件,在终端键入 [shell] photorec [/shell] 然 ...

  5. WP 开发中.xaml 与.xaml.cs

    关于 WP 开发中.xaml 与.xaml.cs 的关系   今天我们先来看一下在WP8.1开发中最长见到的几个文件之间的关系.比较论证,在看这个问题之前我们简单看看.NET平台其他两个不同的框架: ...

  6. Solr高亮与Field权重

    Solr高亮与Field权重   Solr高亮 原理 做搜索时,高亮是很常见的需求,那么Solr肯定也为高亮提供了支持.先解释下Solr高亮的原理,在我们设置了需要高亮显示的Field之后,查询得到的 ...

  7. Mysql高级之事务

    原文:Mysql高级之事务 通俗的说事务: 指一组操作,要么都成功执行,要么都不执行.---->原子性 在所有的操作没有执行完毕之前,其他会话不能够看到中间改变的过程-->隔离性 事务发生 ...

  8. Windows 7上使用HP QC的问题

    C(Quantity Center)是一款不错的测试管理工具,最近把公司的操作系统从Windows XP升级到Windows 7之后,发现登录到QC Server的Addin页面,很多客户端组件不能正 ...

  9. ibatis提示Unable to load embedded resource from assembly "Entity.Ce_SQL.xml,Entity".

    原本以为是xml文件配置错误,尝试无果,最终原因未将xml文件的生成操作选择为嵌入的资源.很无语!

  10. requirejs的config及optimizer r.js配置

    1.怎么处理require.js这些不需要被合并的东西 所有appDir中的文件都会先copy到dir文件中,进行压缩,然后根据build.js中的配置进行相应的合并,包括img等:2.样式合并后原来 ...