WPF使用资源字典组织资源
2011-03-29 00:28 观海看云 阅读(702) 评论(1) 编辑 收藏 举报首先在解决方案资源管理器中添加一个或多个资源词典(资源字典),并向多个资源字典中添加对象资源信息。示例中新建了三个资源字典,并向资源字典中添加了对象资源,代码如下。
第一个资源字典:(第一个资源字典命名为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>
出处:http://www.cnblogs.com/zhangtao/
文章版权归本人所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器