WPF使用资源字典组织资源
2014-11-02 12:16
267 查看
转载:http://blog.163.com/wangzhenguo2005@126/blog/static/371405262010111413321728/
首先在解决方案资源管理器中添加一个或多个资源词典(资源字典),并向多个资源字典中添加对象资源信息。示例中新建了三个资源字典,并向资源字典中添加了对象资源,代码如下。
第一个资源字典:(第一个资源字典命名为MyDictionary1.xaml)
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ImageBrush x:Key="MyBrush1"
TileMode="Tile"
ViewportUnits="Absolute"
Viewport="0 0 30 30"
ImageSource="/image/1.png"
Opacity="0.9"/>
</ResourceDictionary>
第二个资源字典:(第二个资源字典命名为MyDictionary2.xaml)
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<LinearGradientBrush x:Key="MyBrush2">
<LinearGradientBrush.GradientStops>
<GradientStopCollection>
<GradientStop Color="Red" Offset="0.1"/>
<GradientStop Color="Green" Offset="0.5"/>
<GradientStop Color="Blue" Offset="0.9"/>
</GradientStopCollection>
</LinearGradientBrush.GradientStops>
</LinearGradientBrush>
</ResourceDictionary>
第三个资源字典:(第三个资源字典命名为MyDictionary3.xaml)
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<SolidColorBrush x:Key="MyBrush3" Color="Blue"/>
</ResourceDictionary>
在创建了多个资源字典后可以将它们合并到主窗体的资源集合中,示例代码如下。
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="MyDictionary1.xaml"/>
<ResourceDictionary Source="MyDictionary2.xaml"/>
<ResourceDictionary Source="Mydictionary3.xaml"/>
</ResourceDictionary.MergedDictionaries>
<ImageBrush x:Key="MyBrush"
TileMode="Tile"
ViewportUnits="Absolute"
Viewport="0 0 50 50"
ImageSource="/image/1.png"
Opacity="0.9"/>
</ResourceDictionary>
</Window.Resources>
将多个资源字典和并到窗体资源集合后,就可以像使用窗体中定义的资源一下使用多个资源字典中的资源了,示例代码如下。
<Window x:Class="WPF中的对象集合.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="MyDictionary1.xaml"/>
<ResourceDictionary Source="MyDictionary2.xaml"/>
<ResourceDictionary Source="Mydictionary3.xaml"/>
</ResourceDictionary.MergedDictionaries>
<ImageBrush x:Key="MyBrush"
TileMode="Tile"
ViewportUnits="Absolute"
Viewport="0 0 50 50"
ImageSource="/image/1.png"
Opacity="0.9"/>
</ResourceDictionary>
</Window.Resources>
<Grid>
<Button Background="{StaticResource MyBrush1}"></Button>
</Grid>
</Window>
相关文章推荐
- WPF使用资源字典组织资源
- wpf使用资源字典组织资源
- wpf使用资源字典组织资源
- WPF使用资源字典组织资源
- WPF资源字典使用
- WPF资源字典使用
- WPF资源字典使用
- Windows Phone 7 定义和使用字典资源(ResourceDictionary)
- WPF嵌入式资源使用方法
- Windows Phone 7 定义和使用字典资源(ResourceDictionary)
- 在 WPF and Silverlight 中创建和消耗资源字典
- wpf和wp7版网络音乐播放器,使用虾米网的资源
- WPF 文件级资源(类似与使用CSS文件,然后引用CSS文件)
- WPF应用程序使用资源及多语言设置学习
- WPF-使用对象资源
- 解决WPF BitmapImage图片资源无法删除,文件正在被另一个进程使用
- WPF BitmapImage 图片资源无法删除,文件正在被另一个进程使用
- 避免让WPF资源字典变得杂乱臃肿
- WPF应用程序使用资源及多语言设置学习-1
- WPF学习笔记(5):合并资源字典