WPF中的“资源”
阅读原文时间:2023年07月10日阅读:1

WPF中的“资源”

WPF中的资源的概念有点类似 web 技术中的静态资源的概念。可以是一个样式,也可以是一个button的边框设置集合。

可以简单的将资源分为如下几个类别:

  • 窗体资源:顾名思义,仅可在当前窗体中使用
  • 全局资源:相对于窗体资源而言,是一个全局级别的,可以被多个窗体引用,可以根据不同的维度定义多个全局资源文件
  • 动态资源:“值”可以被改变的资源,例如:程序启动的时候button的边框是红色的,当点击某个其他按钮后,将边框变成蓝色

创建

&nbsp; <Window.Resources>
&nbsp; &nbsp; &nbsp; &nbsp; <SolidColorBrush x:Key="SolidColor" &nbsp;Color="Red">
&nbsp; &nbsp; &nbsp; &nbsp; </SolidColorBrush>
&nbsp; &nbsp; </Window.Resources>

引用

使用花括号和关键字 StaticResource

&nbsp;<StackPanel>
&nbsp; &nbsp; &nbsp; &nbsp; <Button Content="我是一个按钮" Margin="10" &nbsp;BorderBrush="{StaticResource &nbsp;SolidColor}"></Button>
&nbsp;</StackPanel>

创建 资源字典 文件

右键工程,点击添加-资源字典,命名为  DictionaryButton.xaml

编写全局资源

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
&nbsp; &nbsp; <SolidColorBrush x:Key="GlobalSolidColor" Color="Yellow"></SolidColorBrush>
&nbsp; &nbsp; <Style x:Key="DefaultButtonStyle" TargetType="Button">
&nbsp; &nbsp; &nbsp; &nbsp; <Setter &nbsp; Property="Foreground" &nbsp;Value="Blue" &nbsp;></Setter>
&nbsp; &nbsp; &nbsp; &nbsp; <Setter &nbsp; Property="FontSize" &nbsp;Value="20" &nbsp;></Setter>
&nbsp; &nbsp; &nbsp; &nbsp; <Setter &nbsp;Property="BorderBrush" Value="Pink" ></Setter>
&nbsp; &nbsp; &nbsp; &nbsp; <Setter &nbsp; Property="BorderThickness" &nbsp;Value="10" &nbsp;></Setter>
&nbsp; &nbsp; </Style>
</ResourceDictionary>

引入

在 App.xaml 文件中引入全局资源文件 DictionaryButton.xaml

&nbsp; &nbsp; <Application.Resources>
&nbsp; &nbsp; &nbsp; &nbsp; <ResourceDictionary>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <ResourceDictionary.MergedDictionaries>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <ResourceDictionary Source="DictionaryButton.xaml"></ResourceDictionary>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </ResourceDictionary.MergedDictionaries>
&nbsp; &nbsp; &nbsp; &nbsp; </ResourceDictionary>
&nbsp; &nbsp; </Application.Resources>

引用

&nbsp; &nbsp;<StackPanel>
&nbsp; &nbsp; &nbsp; &nbsp; <Button Content="我是一个按钮" Margin="10" &nbsp; Style="{StaticResource DefaultButtonStyle}"></Button>
&nbsp; &nbsp;</StackPanel>

就资源本身而言,动态资源并没有什么特殊之处,仅仅是在处理方式上面的差异。

创建

参考 窗体资源

引用

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <StackPanel>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Button Content="改变下面控件的边框颜色" Margin="10" &nbsp;Click="Button_Click" ></Button>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Button Content="我是一个按钮" Margin="10" &nbsp;BorderBrush="{DynamicResource &nbsp;SolidColor}" BorderThickness="10"></Button>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </StackPanel>

动态编辑资源

&nbsp; &nbsp;private void Button_Click(object sender, RoutedEventArgs e)
&nbsp; &nbsp; &nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.Resources["SolidColor"] = new SolidColorBrush(Colors.Blue);
&nbsp; &nbsp; &nbsp; &nbsp; }

https://github.com/Naylor55/WPF-Taste/tree/main/resource/ResourceTaste

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器