首页 > 其他分享 >静态资源(StaticResource)和动态资源(DynamicResource)

静态资源(StaticResource)和动态资源(DynamicResource)

时间:2022-09-29 14:59:57浏览次数:50  
标签:StaticResource 静态 public DynamicResource MainWindow 资源

静态资源( StaticResource )指的是在程序载入内存时对资源的一次性使用,之后就不再访问这个资源了;
动态资源(DynamicResource)使用指的是在程序运行过程中然会去访问资源。

 简单的可以理解为,如果换皮肤而不重启程序,就需要用 DynamicResource
<Window x:Class="WpfApp1.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" 
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Window.Resources>
        <SolidColorBrush x:Key="PrimaryColorBrush" Color="#FF2477F3"/>
    </Window.Resources>
    <Grid>
        <StackPanel>
            <Border Margin="10" Width="60" Height="60" Background="{StaticResource PrimaryColorBrush}"></Border>
            <Border Margin="10" Width="60" Height="60" Background="{DynamicResource PrimaryColorBrush}"></Border>
            <Button Margin="10" Width="60" Height="30" Content="Update" Click="Update_Click" />
        </StackPanel>
    </Grid>
</Window> 

 

namespace WpfApp1
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Update_Click(object sender, RoutedEventArgs e)
        { 
            this.Resources["PrimaryColorBrush"] = new SolidColorBrush(Colors.Red);
        }
    }
}

 

 

 

 

 

标签:StaticResource,静态,public,DynamicResource,MainWindow,资源
From: https://www.cnblogs.com/vipsoft/p/16741533.html

相关文章