详解WPF中的对象资源
作者:杜文龙 发布时间:2023-10-28 14:09:41
目录
资源定义好之后,再使用时,可以指定以静态的方式使用资源,还是以动态的方式使用资源。
资源我们都会使用了,接下来需要归类整理我们的资源,使用资源字典:
跨程序集使用资源:这个对象资源跨程序集使用。
在WPF中,所有继承自FrameworkElement的元素都包含一个Resources属性,这个属性就是我们这篇要讲的资源。
这一篇讲解的资源是不是上一篇的程序集资源(那个是在编译过程中打包到程序集中),这个是资源是我们想在公共的地方写一个对象让其他元素重复使用。
先贴个例子:
<Window x:Class="NETResource.MainWindow"
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:NETResource"0
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.Resources>
<SolidColorBrush x:Key="RedBrushButtonBackground" Color="Red" />
</Window.Resources>
<Grid>
<Grid.Resources>
<SolidColorBrush x:Key="BlueBrushButtonBackground" Color="Blue"/>
</Grid.Resources>
<StackPanel>
<Button Width="120" Background="{StaticResource RedBrushButtonBackground}" Content="我是按钮A"/>
<Button Width="120" Background="{StaticResource BlueBrushButtonBackground}" Content="我是按钮B"/>
</StackPanel>
</Grid>
</Window>
显示效果:
从例子中我们看几个资源的关键点,我们再Window元素和Grid元素下添加两个颜色画刷资源,使用x:key标识。上面2个Button都使用了2个不同层级的父元素资源。WPF中有个设计比较好的地方就是元素可以使用父元素的资源。这样我们都把资源放在Window节点下,公用这些资源。
资源定义好之后,再使用时,可以指定以静态的方式使用资源,还是以动态的方式使用资源。
差别就是静态资源只从资源集合获取对象一次,对象的任何变化都会得到消息,而动态资源每次需要用到对象时都会重新从资源集合中查找对象。注意动态资源是每次需要用到对象时才会去资源集合查找,所以在资源非常大,且非常复杂的时候,可以用动态资源的方式可以提高第一次加载窗口的速度(因为没有解析资源标记的过程),其他任何使用都不建议使用动态资源。使用数据绑定去实现。
<Window x:Class="NETResource.Window1"
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:NETResource"
mc:Ignorable="d"
Title="Window1" Height="450" Width="800">
<Window.Resources>
<SolidColorBrush x:Key="ButtonBrushBackground" Color="DarkBlue"/>
</Window.Resources>
<Grid>
<StackPanel Width="120">
<Button Content="按钮A静态资源" Background="{StaticResource ButtonBrushBackground}"/>
<Button Content="按钮B动态资源" Background="{DynamicResource ButtonBrushBackground}"/>
<Button Content="点击切换资源对象" Click="SetButtonBackgroudButton_OnClicked"/>
<Button Content="点击修改资源的值" Click="UpdataButtonBackgroundButton_OnClicked"/>
</StackPanel>
</Grid>
</Window>
using System.Windows;
using System.Windows.Media;
namespace NETResource
{
/// <summary>
/// Window1.xaml 的交互逻辑
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
private void SetButtonBackgroudButton_OnClicked(object sender, RoutedEventArgs e)
{
//修改资源指向的对象。只有动态资源会受到影响,因为动态资源每次使用值的时候,都会重新读取。静态资源不会,所以静态资源不受影响。
//修改样式1
SolidColorBrush brush = new SolidColorBrush(Colors.Red);
brush.Opacity = 0.3;
this.Resources["ButtonBrushBackground"] = brush;
//修改样式2
// LinearGradientBrush linear = new LinearGradientBrush(Colors.Red, Colors.Blue, 90.0);
// this.Resources["ButtonBrushBackground"] = linear;
}
private void UpdataButtonBackgroundButton_OnClicked(object sender, RoutedEventArgs e)
{
//修改资源对象的值,资源没有改变,只是改变了资源的值,所以2个按钮都受到影响。
var brush = this.Resources["ButtonBrushBackground"];
if (brush is SolidColorBrush solidColorBrush)
{
solidColorBrush.Color = Colors.LightBlue;
}
}
}
}
如果有些资源是所有窗体都共享的,建议写在Application的.Resources下。
<Application x:Class="NETResource.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:NETResource"
StartupUri="Window1.xaml">
<Application.Resources>
<SolidColorBrush x:Key="TitleBrush" Color="Red"/>
</Application.Resources>
</Application>
<Window x:Class="NETResource.Window1"
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:NETResource"
mc:Ignorable="d"
Title="WPF教程九:理解WPF中的资源"
Height="450" Width="800">
<Window.Resources>
<SolidColorBrush x:Key="ButtonBrushBackground" Color="DarkBlue"/>
</Window.Resources>
<Grid>
<TextBlock Text="WPF教程九:理解WPF中的资源" Foreground="{StaticResource TitleBrush}"/>
</Grid>
</Window>
资源我们都会使用了,接下来需要归类整理我们的资源,使用资源字典:
我们在工程上右键=》添加=》资源字典=》设置名字为AppBrushes.xaml=》保存
添加代码如下:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:NETResource">
<SolidColorBrush x:Key="DictionaryTitleBrush" Color="Beige"/>
</ResourceDictionary>
在App.xaml中添加对资源字典的使用:
<Application x:Class="NETResource.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:NETResource"
StartupUri="Window1.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="AppBrushes.xaml"/>
</ResourceDictionary.MergedDictionaries>
<SolidColorBrush x:Key="TitleBrush" Color="Red"/>
</ResourceDictionary>
</Application.Resources>
</Application>
这样资源字典就可以被访问了。
我们创建一个button按钮使用资源字典内的样式
<Button Content="使用资源字典下的画刷" Background="{StaticResource DictionaryTitleBrush}"/>
跨程序集使用资源:这个对象资源跨程序集使用。
一定要分清楚,什么是二进制资源(程序集资源持久化),什么是对象资源(公共部分重复使用的对象)。我们手动创建引用其他库的资源字典。
在新建资源DLL的时候,我没有找到直接新建添加引用之后的类库,所以我用以下2种方法种的一种来创建程序集,我使用的是第一种:
1)创建WPF程序,然后删除他下面的App.config、App.xaml、MainWindow.xaml 和Properties下的Rsources.resx、Settings.settings,工程右键=》属性=》应用程序=》输出类型=》类库。用于保留自动对PresentationCore、PresentationFramlework、WindowsBase的引用。
2)添加类库程序,然后添加=》引用=》PresentationCore、PresentationFramlework、WindowsBase。这三个的引用。
添加DLL完毕后,在窗体程序中添加对DLL库的引用。整个目录引用关系结构如下:
现在开始写代码,
首先是ResourceLibrary库。这个是我们的资源库,里面存放我们的资源文件。目前是一个xaml的资源字典。需要我们新建出来。
代码如下:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:ResourceLibrary">
<SolidColorBrush x:Key="ReusableTitle" Color="Yellow"/>
</ResourceDictionary>
而后是引用的App,在App.xaml下添加跨程序集的资源字典(使用pack: URI):
<Application x:Class="WPFUsingResourceLib.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WPFUsingResourceLib"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/ResourceLibrary;component/ReusableDictionary.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
在Window窗体中使用以添加引用的资源:
<Window x:Class="WPFUsingResourceLib.MainWindow"
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:WPFUsingResourceLib"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Button VerticalAlignment="Top" HorizontalAlignment="Left" Width="180" Height="30" Content="我是跨程序集使用资源" Background="{StaticResource ReusableTitle}"/>
</Grid>
</Window>
效果图如下:
好啦,这一篇就写这么多把。主要是就是对象资源的使用,静态和动态资源的差别,跨程序集资源,元素可以使用父类资源。这篇主要是理解就行,后面会写软件,用于演示如何更好的使用这些内容。
来源:https://www.cnblogs.com/duwenlong/archive/2021/04/07/14612070.html


猜你喜欢
- 在工作中经常读写文本文件,在读文件时,需要按开头的两个字节判断文件格式,然后按该格式读文件中的内容。 写文件时,也要按目标文件指定
- 本文实例为大家分享了SpringMVC实现文件上传与下载的具体代码,供大家参考,具体内容如下0.环境准备1.maven依赖<depen
- 前言:自定义View可以分为两种方式:第一种通过继承ViewGroup,内部通过addView的方式将其他的View组合到一起。第二种则是通
- 为什么使用JUnit5JUnit4被广泛使用,但是许多场景下使用起来语法较为繁琐,JUnit5中支持lambda表达式,语法简单且代码不冗余
- 一、背景今天有小伙伴面试的时候被问到:Spring AOP中JDK 和 CGLib * 哪个效率更高?二、基本概念首先,我们知道Sprin
- 本文实例讲述了java实现列表、集合与数组之间转化的方法。分享给大家供大家参考。具体实现方法如下:package test; i
- ArrayList和LinkedList都实现了List接口,有以下的不同点:1、ArrayList是基于索引的数据接口,它的底层是数组。它
- 最近在机顶盒上做一个gridview,其焦点需要在item的子控件上,但gridview的焦点默认在item上,通过android:desc
- SpringBoot遇到的坑@Qualifier报红今天写项目的时候@Qualifier一直报红,排查半天后面才知道原来是idea生成项目的
- 最近单位又有一个新Java项目。涉及到扫码登录。之前项目使用的是 ajax轮询的方式。感觉太low了。所以这次用webSocket的方式进行
- 1.引入依赖 <!--mybatisplus依赖--> <dependency> &nbs
- 本文实例讲述了C#实现的海盗分金算法。分享给大家供大家参考,具体如下:海盗分金的故事5个海盗抢到了100颗宝石,每一颗都一样的大小和价值连城
- 这篇文章主要介绍了Java数据封装树形结构代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可
- 本文实例为大家分享了Android实现手机多点触摸画圆的具体代码,供大家参考,具体内容如下静态效果图:(多个手指按下和抬起的状态)代码实现部
- 1、多态性多态性是面向对象的最后一个特征,它本身主要分为两个方面: 方法的多态性:重载与覆写1、重载:同一个方法名称,根据参数类型以及个数完
- 我们还是用一个小例子来看看自定义View和自定义属性的使用,带大家来自己定义一个带进度的圆形进度条,我们还是先看一下效果吧从上面可以看出,我
- 本文实例讲述了JAVA中的final关键字用法。分享给大家供大家参考,具体如下:根据上下文环境,java的关键字final也存在着细微的区别
- java Long类型转为String类型1、Long.ValueOf("String")返回Long包装类型数据包装类
- 在这篇文章中,我将向您展示如何用新的Java 8 forEach语句循环一个List和Map。1、forEach 和 Map1.1、常规循环
- 继承和多态派生类具有基类所有非私有数据和行为以及新类自己定义的所有其他数据或行为,即子类具有两个有效类型:子类的类型和它继承的基类的类型。对