WPF整理-使用ResourceDictionary管理Logical Resources
2013-07-02 22:03
337 查看
“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:
2.在App.xaml中Merge则个Resource。
“Open App.xaml. We need to merge external resource dictionaries into the main
application dictionary.”
打开App.xaml,添加如下内容:
3.这样我们就可以在页面中正常使用了。
效果如下:
-----------------------------------
在实际开发中更常用的做法是:直接在使用的View内部Merge。
效果同上,如下:
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整理-使用逻辑资源
- [转]WPF中RichTextBox的使用小窍门(翻译、整理)
- WPF整理-使用用户选择主题的颜色和字体
- WPF中RichTextBox的使用小窍门(翻译、整理)
- linux下vi命令修改文件及保存等使用方法整理
- .NetCore中EFCore的使用整理(二)-关联表查询
- SVN CornerStone的使用 整理
- 使用WPS快速整理中英混排的网页文字
- <转载>拒绝卡顿——在WPF中使用多线程更新UI
- 使用双缓冲让界面绘制图形时避免闪烁 整理
- ESP8266使用说明 整理
- 【知识点整理(Java)】Java Thread 简单使用注意点汇总(一)
- windows下mongodb安装与使用整理
- VIM的一些自己的使用tips(5年前整理的,看到后热泪盈眶)
- 深入浅出WPF之Binding的使用(一)
- windows 重装系统后必装的软件(根据个人使用情况整理,欢迎读者在评论推荐其他好用的软件)
- android studio基本使用零碎整理
- WPF入门教程系列(二) 深入剖析WPF Binding的使用方法
- MYSQL日常使用整理
- 【Java】IO整理之File类使用大全