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的更多相关文章
- 使用ResourceDictionary管理Logical Resources
WPF整理-使用ResourceDictionary管理Logical Resources “Logical resources may be of various types, such as br ...
- WPF整理-使用逻辑资源
"Traditional application resources consist of binary chunks of data, typically representing thi ...
- WPF整理-Style
"Consistency in a user interface is an important trait; there are many facets of consistency, ...
- WPF整理-XAML构建后台类对象
1.XAML 接触WPF的第一眼就是XAML---XAML是用来描绘界面的.其实不然! "Actually, XAML has nothing to do with UI. It's mer ...
- WPF整理-使用用户选择主题的颜色和字体
“Sometimes it's useful to use one of the selected colors or fonts the user has chosen in theWindows ...
- WPF整理-自定义一个扩展标记(custom markup extension)
"Markup extensions are used to extend the capabilities of XAML, by providing declarativeoperati ...
- WPF整理-XAML访问静态属性
"XAML provides an easy way to set values of properties—type converters and the extended propert ...
- WPF整理--动态绑定到Logical Resource
“What happens if we replace aspecific resource? Would that be reflected in all objects using the res ...
- WPF整理-二进制资源和内容
WPF中的Binary Resource(二进制资源)是相对于前面所说的Logical resource(逻辑资源)而说的,一般指Image.XML文件等. 注意:这里说的是Resource" ...
随机推荐
- Ruby字符串
在Ruby中的String对象持有和操纵的任意序列的一个或多个字节,通常表示人类语言的字符表示.简单的字符串文本括在单引号中,如 'This is a simple Ruby string liter ...
- wpf listview
<Window x:Class="WpfTutorialSamples.ListView_control.ListViewGridViewSample" xml ...
- MATLAB曲面插值及交叉验证
在离散数据的基础上补插连续函数,使得这条连续曲线通过全部给定的离散数据点.插值是离散函数逼近的重要方法,利用它可通过函数在有限个点处的取值状况,估算出函数在其他点处的近似值.曲面插值是对三维数据进行离 ...
- url地址的图片路径
url地址的图片路径: (./images/1.jpg) 中的./指根路径,有或没有都可以: (../images/1.jpg) 中的../指相对路径: (../../images/1.jpg) 中的 ...
- word
# word中向下箭头 原文:http://jingyan.baidu.com/article/c1465413b0a9c70bfcfc4cbc.html 使用替换功能,把^l(l是小写的L)替换为^ ...
- Linux C编程学习之开发工具2---GDB调试器
简介 GDB是一个功能强大的交互式程序调试工具,主要工作在字符界面下. GDB不仅可以用来调试C/C++ 语言编写的程序,还可以用来调试 Pascal.Objective-C,以及Fortran等语言 ...
- Tomcat 9.0安装配置(转)
http://www.cnblogs.com/saratearing/p/5811866.html
- Andriod学习笔记2:“Your content must have a ListView whose id attribute is 'android.R.id.list'”问题的解决办法
问题描述 activity_main.xml代码如下: <?xml version="1.0" encoding="utf-8"?> <Lin ...
- OS X 下不通过Homebrew安装ASP.NET 5开发环境
在 ASP.NET 的 Home repo 里,推荐使用 Homebrew 安装开发环境,不过我的电脑里已经有 ports 了,这应该是当年用 rvm 安装 Ruby 时悄悄地装上的吧.不管怎样,作为 ...
- centos配置虚拟主机
首先注释掉 DocumentRoot /var/www/html 然后添加如下代码至文件底部: NameVirtualHost 192.168.0.3 <virtualhos ...