原文: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>

效果如下:

-----------------------------------

在实际开发中更常用的做法是:直接在使用的View内部Merge。

<Window x:Class="WPFMergedDicitonary.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"><Window.Resources><ResourceDictionary><ResourceDictionary.MergedDictionaries><ResourceDictionary Source="Brushes.xaml"/></ResourceDictionary.MergedDictionaries></ResourceDictionary></Window.Resources><Grid><Ellipse Fill="{StaticResource brush1}"/></Grid>
</Window>

效果同上,如下:

WPF整理-使用ResourceDictionary管理Logical Resources相关推荐

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

    WPF整理-使用逻辑资源 原文:WPF整理-使用逻辑资源 "Traditional application resources consist of binary chunks of dat ...

  2. WPF整理-为User Control添加依赖属性

    WPF整理-为User Control添加依赖属性 原文:WPF整理-为User Control添加依赖属性 依赖属性 ".NET properties are nothing more t ...

  3. wpf控件样式管理示意图

    原文:wpf控件样式管理示意图

  4. WPF整理-处理没有注意到的异常

    在.NET中,我们使用try-catch-finally来处理异常.但,当一个Exception抛出,抛出Exception的代码又没有被try包围时,程序就崩溃了. 这些异常往往是你没有注意到的.在 ...

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

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

  6. python下载包管理器_Python包管理整理:setuptool管理python相关的包

    setuptool管理python相关的包 一.介绍 setuptool管理python相关的包的工具.这些包是zip格式发布,但是后缀一般都是.egg setuptool能解决python包的依赖关 ...

  7. Python包管理整理:setuptool管理python相关的包

    setuptool管理python相关的包 一.介绍 setuptool管理python相关的包的工具.这些包是zip格式发布,但是后缀一般都是.egg setuptool能解决python包的依赖关 ...

  8. 鸟哥linux视频教程密码,[鸟哥linux视频教程整理]04_01_Linux用户管理命令详解

    一.用户管理: 1. 添加用户:useradd [option] USERNAME -u:UID -g:GID(基本组) -G:GID,..(附加组) -c:"COMMENT" - ...

  9. linux资料整理之用户管理

    博客:http://lijinhuan.blog.51cto.com/ 微博:http://weibo.com/lijinhuanexperience 微信:xiaoleetongxue 一.用户账号 ...

最新文章

  1. Cocos2d-x手机游戏开发行业背景分析
  2. SAP SD交货单凭证流里的Confirmation of Service初探 II
  3. 《工业大数据白皮书》2019版正式发布
  4. 全民上网到全民织网 Web 2.0掀起人民战争
  5. mysql数据库的常用操作-索引
  6. 【错误记录】前台进程报错 ( Permission Denial: startForeground requires android.permission.FOREGROUND_SERVICE )
  7. 数据流中的中位数,我轻敌了
  8. 任务二 用户注册界面设计
  9. 基于 abp vNext 和 .NET Core 开发博客项目 - 定时任务最佳实战(一)
  10. 并发调试和JDK8新特性
  11. 亚马逊如何变成 SOA(面向服务的架构)?
  12. 数据插不进mysql_数据插入不进数据库里面去。
  13. X-Pack Spark归档POLARDB数据做分析
  14. flume学习(四):Flume Channel Selectors使用
  15. 卡尔曼滤波(Kalman filtering)算法学习小记
  16. 微型计算机启天m425显卡驱动,Lenovo联想启天M425台式机NVIDIA VGA驱动26.21.14.4223版For Win10-64(2020年4月7日发布)...
  17. C#学习笔记之线程安全
  18. imap java代码,JAVA运用IMAP、POP3、SMTP协议收发邮件
  19. Jmeter应用-接口测试
  20. 微信设置“种草昵称”,个性有意思,来看看!

热门文章

  1. postman使用之四:设置读取变量和切换环境
  2. iOS 让UIButton根据文字内容自动计算宽高
  3. Servlet 3.0对上传的支持
  4. ubuntu 12.04 以固定 IP 地址连接网络并配置DNS
  5. 由sock引起的感想
  6. 汇编和c语言函数的参数传递,c文件汇编后函数参数传递的不同之处
  7. 微信 的微服务器配置,spring-boot wm-accesstoken
  8. js 连mysql 延后_详解MySQL 主从同步延迟
  9. ubuntu安装python3.6_Ubuntu16.04下安装python3.6.4详细步骤
  10. java+基于dom4j工具类_基于dom4j的xml映射实体的工具类(java)