ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

WPF | 跟着视频敲代码11 | 动态资源、静态资源、资源字典

2022-07-20 08:00:56  阅读:161  来源: 互联网

标签:11 App SolidColorBrush Current FindResource SolidColor WPF 资源


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");
        }

  

标签:11,App,SolidColorBrush,Current,FindResource,SolidColor,WPF,资源
来源: https://www.cnblogs.com/dongxizhen/p/16496477.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有