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" ...
随机推荐
- oracleDBA-D4
1.数据字典: 创建和维护的可修改的系统表..它存放有关数据库和数据库对象的信息. 数据字典=基表+字典视图 2.数据字典所存放的信息: 数据库的逻辑和物理结构(如:表空间和数据文件),数据对象定义的 ...
- 文件上传大小js判断
function fileChange(target) { var fileSize = 0; if (isIE && !target.files) { var filePath = ...
- java 代码解压7z(带密码)转载请注明出处,谢谢
<sevenzipjbinding.version>9.20-2.00beta</sevenzipjbinding.version> <dependency> &l ...
- wpf——三维学习1
以下xmal是我从msdn上复制下来的.是用于在wpf中创建3d模型的实例链接https://msdn.microsoft.com/zh-cn/library/ms747437.aspx看它的使用方式 ...
- 【leetcode】Remove Nth Node From End of List
题目简述: Given a linked list, remove the nth node from the end of list and return its head. For example ...
- 7 Container With Most Water_Leetcode
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- tomcat热部署
tomcat默认支持热部署,修改文件会自动加载部署,不需要重启容器 server.xml配置如下所示 autoDeploy=”true” — 自动部署 reloadable=”true” — 自动加载
- UWP 判断windows mobile是使用的实体键还是虚拟按键
最近在写启动屏幕,发现虚拟按钮会挡住,启动屏幕的最下面的元素,大概有50 px.可是有什么办法知道手机是用的实体键还是虚拟按键吗? 如下图.可以看到红色的部分显示了一点点.代码里设置的是60px. 在 ...
- vue-loader配合webpack的使用及安装
vue-loader配合webpack的使用及安装: 工程文件简单的目录结构 index.html main.js 入口文件 App.vue vue文件,官方推荐命名法 package.jso ...
- IMap 对map的功能的强化
为了解决表单提交获得数据的方便性,我们将map的功能进行加强,表单提交的数据会自动将页面数据放入PageData对象中,当从页面获取数据时 new的时候要传request.request.getPar ...