WPF-资源-逻辑资源
逻辑资源是一些存储在元素的Resources属性中的.NET对象。也可以叫做“XAML资源”。
由于FrameworkElement和FrameworkContentElement基类都有这个Resources属性(System.Windows.ResourceDictionary)。
举例:注意控件的Background和BorderBrush属性。
<Window x:Class="WpfApp1.ResourceTest" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:WpfApp1" Background="Yellow" mc:Ignorable="d" Title="ResourceTest" Height="450" Width="800"> <StackPanel> <Button ToolTip="ContentResource" Background="Yellow" BorderBrush="Red" Margin="5"> <Image Height="20" Width="20" Source="/Resource/Content.png"></Image> </Button> <Button ToolTip="Resource" Background="Yellow" BorderBrush="Red" Margin="5"> <Image Height="20" Width="20" Source="/Resource/new.jpg"></Image> </Button> <Button ToolTip="Resource" Background="Yellow" BorderBrush="Red" Margin="5"> <Image Height="20" Width="20" Source="/CommandTest;Component/Content.png"></Image> </Button> </StackPanel> </Window>
使用逻辑资源重新定义控件的样式。资源定义在Resources属性中。
<Window x:Class="WpfApp1.ResourceTest" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:WpfApp1" mc:Ignorable="d" Title="ResourceTest" Height="450" Width="800"> <Window.Resources> <SolidColorBrush x:Key="backgroupBrush">Yellow</SolidColorBrush> <SolidColorBrush x:Key="borderBrush">Red</SolidColorBrush> </Window.Resources> <Window.Background> <StaticResource ResourceKey="backgroupBrush"></StaticResource> </Window.Background> <StackPanel> <Button ToolTip="ContentResource" Background="{StaticResource backgroupBrush}" BorderBrush="{StaticResource borderBrush}" Margin="5"> <Image Height="20" Width="20" Source="/Resource/Content.png"></Image> </Button> <Button ToolTip="Resource" Background="{StaticResource backgroupBrush}" BorderBrush="{StaticResource borderBrush}" Margin="5"> <Image Height="20" Width="20" Source="/Resource/new.jpg"></Image> </Button> <Button ToolTip="Resource" Background="{StaticResource backgroupBrush}" BorderBrush="{StaticResource borderBrush}" Margin="5"> <Image Height="20" Width="20" Source="/CommandTest;Component/Content.png"></Image> </Button> </StackPanel> </Window>
资源查找
StaticResource标记扩展只有一个参数,ResourceKey表示资源字典中项的键值。
标记扩展类实现了遍历逻辑树的能力。首先检查当前元素的的资源字典,如果资源没有找到;他会检查父元素、父父元素,不断向上查找,直到到达根元素为止。如果在程序层面资源没有找到,他会检查系统集合。如果资源最终没有找到,会抛出一个InvalidOperationException异常。
静态资源和动态资源
WPF提供两种访问逻辑资源的方式
- 一种是静态的,由StaticResource实现,这种资源仅会被应用一次(在第一次加载资源时)。
- 一种是动态资源,由DynamicResource实现,资源每次更改时都会被重新应用。
区别分析
静态资源和动态资源的主要区别。资源的更新会反映在使用了动态资源的元素上。
性能区别,动态资源需要跟踪变化,占用更多的资源。
加载时间,静态资源必在Window或Page加载之后加载;动态资源在实际使用时加载。
使用范围,动态资源只能用于设置依赖属性值,静态资源可以在任何地方使用。
使用区别,静态资源在引用前必须声明,动态资源不用。
资源整合
资源可以分别在多个文件(ResourceDictionary)中定义。在使用时引用多个文件整合资源;如果有同名资源,按引用顺序保留最后一个引用。
<Window.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="Resource/Dictionary1.xaml"></ResourceDictionary> </ResourceDictionary.MergedDictionaries> <SolidColorBrush x:Key="backgroupBrush">Yellow</SolidColorBrush> <SolidColorBrush x:Key="borderBrush">Red</SolidColorBrush> </ResourceDictionary> </Window.Resources>
程序代码中定义和应用资源
添加资源
window.Resources.Add("backgroundBrush",new SolidColorBrush("Yellow"));
使用资源
设置静态资源,button.Backgroup = (Brush)button.FindResource("backgroupBrush");
设置动态资源,button.SetResourceReference(Button.BackgroupProperty,"backgroupBrush");
跨程序集访问逻辑资源
逻辑资源定义:
<SolidColorBrush x:Key="{ComponentResourceKey TypeInTargetAssembly={x:Type local:MyClass}, ResourceId=MyClassBrush}"></SolidColorBrush>
使用定义
<Button ToolTip="Resource" Background="{DynamicResource {ComponentResourceKey TypeInTargetAssembly=otherAssembly:MyClass,ResourceId=MyClassBrush}}" </Button>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】