无废话WPF系列16:资源
2011-02-27 11:13 敏捷的水 阅读(1454) 评论(0) 编辑 收藏 举报在WPF中资源通常用作“样式”(Style)、样式模板、数据模板等。
一、资源的定义及使用
1. 应用程序级资源:
定义在App.xaml文件中,作为整个应用程序共享的资源
1 2 3 4 5 6 7 8 | < Application x:Class="DeepXAML.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" StartupUri="MainWindow.xaml"> < Application.Resources > < SolidColorBrush Color="Red" x:Key="redBrush"></ SolidColorBrush > </ Application.Resources > </ Application > |
使用应用程序集资源
1 2 3 4 5 6 7 8 9 10 | < Window x:Class="DeepXAML.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:DeepXAML" xmlns:sys="clr-namespace:System;assembly=mscorlib" Title="MainWindow" Height="250" Width="450"> < StackPanel x:Name="stackPanel"> < Button Background="{StaticResource ResourceKey=redBrush}">test app resource</ Button > </ StackPanel > </ Window > |
2. 窗体级资源:定义在Window或Page中,作为一个窗体或页面共享的资源存在
1 2 3 4 5 6 7 8 9 10 11 12 13 | < Window x:Class="DeepXAML.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:DeepXAML" xmlns:sys="clr-namespace:System;assembly=mscorlib" Title="MainWindow" Height="250" Width="450"> < Window.Resources > < SolidColorBrush Color="Red" x:Key="redBrush"></ SolidColorBrush > </ Window.Resources > < StackPanel x:Name="stackPanel"> < Button Background="{StaticResource ResourceKey=redBrush}">test app resource</ Button > </ StackPanel > </ Window > |
3. 文件级资源:定义在资源字典的XAML文件中,再引用
在Visual Studio的WPF应用程序项目中,添加“资源字典(Resource Dictionary)”类型的项
1 2 3 4 | < ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> < SolidColorBrush Color="Red" x:Key="redBrush"></ SolidColorBrush > </ ResourceDictionary > |
1 2 3 4 5 6 7 8 9 10 11 12 13 | < Window x:Class="DeepXAML.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:DeepXAML" xmlns:sys="clr-namespace:System;assembly=mscorlib" Title="MainWindow" Height="250" Width="450"> < Window.Resources > < ResourceDictionary Source="Skin1.xaml"></ ResourceDictionary > </ Window.Resources > < StackPanel x:Name="stackPanel"> < Button Background="{StaticResource ResourceKey=redBrush}">test app resource</ Button > </ StackPanel > </ Window > |
4.对象(控件)级资源:定义在某个ContentControl中,作为其子容器、子控件共享的资源
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | < Window x:Class="DeepXAML.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:DeepXAML" xmlns:sys="clr-namespace:System;assembly=mscorlib" Title="MainWindow" Height="250" Width="450"> < StackPanel x:Name="stackPanel"> < StackPanel.Resources > < ResourceDictionary Source="Skin1.xaml"></ ResourceDictionary > </ StackPanel.Resources > < Button Background="{StaticResource ResourceKey=redBrush}">test app resource</ Button > </ StackPanel > </ Window > 二、资源文件解析的顺序 |
1 | 这个顺序和层叠样式表类似,优先级从高到底为:对象级,窗体级,应用程序集。静态资源(StaticResource)和动态资源(DynamicResource) |
资源可以作为静态资源或动态资源进行引用。这是通过使用 StaticResource 标记扩展或 DynamicResource 标记扩展完成的。
通常来说,不需要在运行时更改的资源使用静态资源;而需要在运行时更改的资源使用动态资源。动态资源需要使用的系统开销大于静态资源的系统开销。
三、静态资源(StaticResource)和动态资源(DynamicResource)
资源可以作为静态资源或动态资源进行引用。这是通过使用 StaticResource 标记扩展或 DynamicResource 标记扩展完成的。
通常来说,不需要在运行时更改的资源使用静态资源;而需要在运行时更改的资源使用动态资源。动态资源需要使用的系统开销大于静态资源的系统开销。
Background="{DynamicResource redBrush}"
private void Button_Click(object sender, RoutedEventArgs e)
{
SolidColorBrush brush = new SolidColorBrush(Colors.Green);
this.Resources["redBrush"] = brush;
}
扫码关注公众号,了解更多管理,见识,育儿等内容

出处:http://www.cnblogs.com/cnblogsfans
版权:本文版权归作者所有,转载需经作者同意。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· 展开说说关于C#中ORM框架的用法!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
2008-02-27 ten sentences(91-100)
2008-02-27 ten sentences(81-90)
2008-02-27 ten sentences(71-80)
2008-02-27 ten sentences(61-70)
2008-02-27 ten sentences(51-60)