WPF(资源、资源字典、样式、URL路径、嵌入资源) 二、资源定义资源就是类型实例化之后的对象。每一个控件都有一个Resources属性该属性存储了一个资源字典集合。2.1资源查找顺序和作用范围位置作用范围示例Application.ResourcesApp.xaml全局所有窗口Window.Resources当前窗口仅该 Window 内控件UserControl.Resources当前用户控件仅该 UserControl 内控件的Resources如 Grid.Resources局部仅子元素可见资源查找顺序控件自身 → 父容器 → Window → Application → 系统默认。2.2静态资源和动态资源只是控件的使用方式不同静态资源只在运行时加载一次动态资源随资源改变而改变示例Window.Resources SolidColorBrush x:Keyaaa ColorRed/SolidColorBrush /Window.Resources Grid Grid.RowDefinitions RowDefinition Height*/ RowDefinition Height*/ /Grid.RowDefinitions Button Background{StaticResource aaa} 111/Button Button Background{DynamicResource aaa} Grid.Row1 ClickButton_Click111/Button /Gridcs文件改变资源aaa对应的值动态资源更新静态资源不变private void Button_Click(object sender, RoutedEventArgs e) { this.Resources[aaa] new SolidColorBrush(Colors.Yellow); }2.3资源管理形式2.3.1窗口中直接定义这里Resources中的内容以键对值形式加入但是实际上形成了一个字典Window.Resources SolidColorBrush x:KeyWindowBackgroundBrush Color#2bb6fe / Style TargetTypeButton Setter PropertyBackground ValueAliceBlue/ /Style /Window.Resources2.3.2appxaml中定义和窗口一样的写法只是作用域发生改变Application.Resources SolidColorBrush x:KeyWindowBackgroundBrush Color#2bb6fe / Style TargetTypeButton Setter PropertyBackground ValueAliceBlue/ /Style /Application.Resources2.3.3资源字典引入Window.Resources //这里的Resources属性本身就是ResourceDictionary类的引用类型这里相当于传给他一个对象 ResourceDictionary //mergedDictionaries是一个connect集合多个集合合并时相同只保留一个 ResourceDictionary.MergedDictionaries ResourceDictionary SourceDictionary1.xaml/ /ResourceDictionary.MergedDictionaries /ResourceDictionary /Window.Resources定义项目右键创建资源字典ResourceDictionary xmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:xhttp://schemas.microsoft.com/winfx/2006/xaml SolidColorBrush x:Keyaaa ColorRed/SolidColorBrush /ResourceDictionary资源字典的合并在MergedDictionaries集合中当多个被合并的字典定义了相同的资源键x:Key时最后被加入的字典中的资源生效在同一个ResourceDictionary中直接定义的本地资源内联资源优先级高于所有通过MergedDictionaries合并进来的资源样式styleTargetType属性这是一个类类型也就是一个反射这个属性指明了当前样式要作用于哪种类型的控件上。因为WPF中有许多的控件我们定义一个样式时必须要指明这个样式的“适用范围”。BasedOn属性样式也有继承的概念所以BasedOn指明了当前样式继承于哪个样式Triggers属性这是一个集合属性表示触发器的定义当满足某些条件时触发哪些行为以使控件达到一定的“节目效果”。比如当鼠标移上去时控件的背景颜色变成红色。这些的效果就可以通过定义控件的触发器来设置。Setters属性这也是一个集合属性样式是控件属性的值的“提前设置”所以我们对控件属性的值的设置都是以Setter条目的形式一条一条的放到这个Setters集合中。Setter的使用Style TargetTypeTextBox Setter PropertyBorderBrush Value#CCC / Setter PropertyBorderThickness Value1 / Setter PropertyPadding Value4,2 / Setter PropertyMinHeight Value24 / /StyleTargetType与BasedOn的使用!-- 基样式隐式 -- Style TargetTypeButton Setter PropertyPadding Value8,4 / Setter PropertyMinHeight Value28 / Setter PropertyBackground ValueWhite / Setter PropertyBorderBrush Value#999 / /Style !-- 变体 1继承隐式样式只改差异 -- Style x:KeyToolbarButton TargetTypeButton BasedOn{StaticResource {x:Type Button}} Setter PropertyFontSize Value11 / Setter PropertyPadding Value4,2 / Setter PropertyMinHeight Value26 / /Style !-- 变体 2继承 ToolbarButton再加橙色 -- Style x:KeyAccentButton TargetTypeButton BasedOn{StaticResource ToolbarButton} Setter PropertyBackground Value#F5A623 / Setter PropertyBorderBrush Value#D48806 / /Style应用样式Button Margin5,0,0,0 Content清空 Style{StaticResource ToolbarButton} /(1) Property Trigger属性触发器Style TargetTypeButton Setter PropertyBackground ValueLightGray / Style.Triggers Trigger PropertyIsMouseOver ValueTrue Setter PropertyBackground ValueYellow / /Trigger Trigger PropertyIsPressed ValueTrue Setter PropertyBackground ValueRed / /Trigger /Style.Triggers /Style(2) DataTrigger数据触发器根据绑定数据值改变样式Style TargetTypeTextBlock Style.Triggers DataTrigger Binding{Binding IsImportant} ValueTrue Setter PropertyFontWeight ValueBold / Setter PropertyForeground ValueRed / /DataTrigger /Style.Triggers /Style(3) EventTrigger事件触发器常用于动画Style TargetTypeButton Style.Triggers EventTrigger RoutedEventButton.Click BeginStoryboard Storyboard DoubleAnimation Storyboard.TargetPropertyOpacity From1.0 To0.2 Duration0:0:0.3 AutoReverseTrue / /Storyboard /BeginStoryboard /EventTrigger /Style.Triggers /Style原文链接https://www.cnblogs.com/tlnshuju/p/19433520一、Pack URL路径Pack URIPack Uniform Resource Identifier用于统一标识和定位资源的 URI 方案核心作用让你用一种标准方式引用当前程序集中的资源如图片、样式、XAML其他程序集DLL中的资源应用程序安装目录下的外部文件规定必须使用正斜杠/1.1Application Data URI嵌入资源pack://application:,,,/程序集名称;component/资源路径如果省略程序集名称表示当前程序集。component是关键字表示资源在程序集内部当前程序集可省略。1.1.1当前程序集中的资源最常见示例-项目结构1MyApp/ 2├── App.xaml 3└── Assets/Images/logo.png简略写法最常用所有以/开头的相对 URI 在 WPF 中都会被自动解释为pack://application:,,,//作用是回到根目录/Assets/Images/logo.png完整写法pack://application:,,,/Assets/Images/logo.png1.1.2其他程序集类库中的资源示例类库名MyLibrary资源路径Themes/Generic.xaml简写形式仍需保留;componenturiMyLibrary;component/Themes/Generic.xaml完整写法pack://application:,,,/MyLibrary;component/Themes/Generic.xaml1.2Site of Origin URI松散文件用于访问未嵌入程序集、而是放在应用程序部署目录如bin\Debug或安装目录中的文件。1. 语法无简写pack://siteoforigin:,,,/相对路径2. 示例假设你的应用安装在C:\MyApp\目录下有1C:\MyApp\ 2├── MyApp.exe 3└── Data/config.jsonUri uri new Uri(pack://siteoforigin:,,,/Data/config.json);⚠️ 注意不能简写不能写成/Data/config.json那会被当作嵌入资源文件必须存在且 Build Action None或Content并复制到输出目录