WPF:静态、动态资源以及资源词典
WPF:静态、动态资源以及资源词典静态资源与动态资源
我们常常会使用样式或者控件模板放在Window.Resources中,比如这样:
https://img2023.cnblogs.com/blog/3267782/202408/3267782-20240820214416077-1604657412.png
静态资源与动态资源使用如下:
<Window.Resources>
<SolidColorBrush x:Key="SolidColor" Color="#FF0000" />
</Window.Resources>
<Grid>
<StackPanel>
<Button Height="40" Margin="10" Content="Button1" Click="Button_Click" />
<Button Height="40"
Margin="10"
Content="Button2"
BorderBrush="{StaticResource SolidColor}"
BorderThickness="4" />
<Button Height="40"
Margin="10"
Content="Button3"
BorderBrush="{DynamicResource SolidColor}"
BorderThickness="4" />
</StackPanel>
</Grid>区别:动态资源是在界面中根据指令可以变化的,静态则不会
https://img2023.cnblogs.com/blog/3267782/202408/3267782-20240820214837102-1553523374.pnghttps://img2023.cnblogs.com/blog/3267782/202408/3267782-20240820214900618-1580338072.png
资源词典
当样式多了,这个时间我们需要单独创建一个资源去管理他,这个时间我们就有了资源词典:
https://img2023.cnblogs.com/blog/3267782/202408/3267782-20240820215114444-1856509608.png
将Window.Resources中的代码移植过来:
https://img2023.cnblogs.com/blog/3267782/202408/3267782-20240820215242821-1330092325.png这个时间还得在加载时将这个资源词典加载过来,在app.xaml中加载
https://img2023.cnblogs.com/blog/3267782/202408/3267782-20240820215352643-1148780232.png在main中也可以查找对应的资源,如下:
https://img2023.cnblogs.com/blog/3267782/202408/3267782-20240820215508918-1751461568.png结果依然可以得到:
https://img2023.cnblogs.com/blog/3267782/202408/3267782-20240820215718569-199298948.png
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页:
[1]