上善若水

水善利万物而不争
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

https://www.bilibili.com/video/BV1nY411a7T8?p=12

点击Update 更新动态资源 DynamicResource按钮的边框颜色发生变动 StaticResource不变

StaticResource 静态资源初始加载后就不再随着资源属性的变动而变动
DynamicResource 动态资源初始加载后资源属性的变动会跟着变动
this.Resources["SolidColor"] = new SolidColorBrush(Colors.Black); 后端通过Key查找到资源

 

 

   <Window.Resources>
        
        <SolidColorBrush x:Key="SolidColor" Color="Red"></SolidColorBrush>
    </Window.Resources>
    <Grid>
        <StackPanel>
            <Button Margin="10" Content="Update" Click="Button_Click"></Button>
            <Button Margin="10"  Height="30" BorderThickness="5" Content="StaticResource"     BorderBrush="{StaticResource SolidColor}" ></Button>
            <Button Margin="10"  Height="30" BorderThickness="5" Content="DynamicResource"  BorderBrush="{DynamicResource SolidColor}" ></Button>
          
        </StackPanel> 
    </Grid>

  

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

 

 

 对于多样式避免重复多次定义可以使用资源字典

 

 

 DictionaryButton.xaml代码如下:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 
    <SolidColorBrush x:Key="SolidColor" Color="Red"></SolidColorBrush>
    <Style  TargetType="Button" x:Key="DefaultButtonStyle"> 
        <Setter Property="Height" Value="30"></Setter>
        <Setter Property="Margin" Value="5,0"></Setter>
        <Setter Property="Background" Value="Blue"></Setter> 
        <Setter Property="BorderThickness" Value="5"></Setter> 
    </Style>
</ResourceDictionary>

 

在App.xaml 中添加引用  

<Application x:Class="WPF04.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:WPF04"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <ResourceDictionary> 
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="DictionaryButton.xaml"></ResourceDictionary>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

页面中Resource就可以删除了

<Window x:Class="WPF04.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:WPF04"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Window.Resources> 
    </Window.Resources>
    <Grid>
        <StackPanel>
            <Button  Margin="10" Content="Update" Click="Button_Click"></Button>
            <Button  Content="StaticResource"     BorderBrush="{StaticResource SolidColor}" ></Button>
            <Button  Content="DynamicResource"  BorderBrush="{DynamicResource SolidColor}" ></Button> 
        </StackPanel> 
    </Grid>
</Window>

 后端查找资源的方式 通过 App.Current.FindResource(key)

 private void Button_Click(object sender, RoutedEventArgs e)
        {
            this.Resources["SolidColor"] = new SolidColorBrush(Colors.Black);
            //通过 App.Current.FindResource(key)可以找到资源字典中的样式
            var SolidColor = App.Current.FindResource("SolidColor");
            var DefaultButtonStyle = App.Current.FindResource("DefaultButtonStyle");
        }