一、概述
WPF资源用于保存画刷、样式或模版。
尽管可在代码中创建和操作资源,但通常在XAML标记中定义资源。
每个元素都有Resources属性,该属性存储一个资源字典集合。尽管每个元素都有Resources属性,但通常在窗口级别定义资源,这是因为窗口内的其它元素都能访问到定义的资源。
<Window x:Class="ResourcesDemo.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:ResourcesDemo"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<ImageBrush x:Key="TileBrush" TileMode="Tile" ViewportUnits="Absolute" Viewport="0 0 32 32"
ImageSource="/Images/smile.png" Opacity="0.3">
</ImageBrush>
</Window.Resources>
<StackPanel>
<Button Background="{StaticResource TileBrush}" Margin="5" Padding="5" FontWeight="Bold" FontSize="14">A Tiled Button</Button>
<Button Margin="5" Padding="5" FontWeight="Bold" FontSize="14">A Normal Button</Button>
<Button Background="{StaticResource TileBrush}" Margin="5" Padding="5" FontWeight="Bold" FontSize="14">Another Tiled Button</Button>
</StackPanel>
</Window>
区别:
①静态资源只从资源集合中获取对象一次。
②动态资源在每次需要对象时都会重新从资源集合中查找对象。这意味着可在同一个键下放置一个全新的对象,并且动态资源会应用该变化。
<Window x:Class="ResourcesDemo.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:ResourcesDemo"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<ImageBrush x:Key="TileBrush" TileMode="Tile" ViewportUnits="Absolute" Viewport="0 0 32 32"
ImageSource="/Images/smile.png" Opacity="0.3">
</ImageBrush>
</Window.Resources>
<StackPanel>
<Button Background="{StaticResource TileBrush}" Margin="5" Padding="5" FontWeight="Bold" FontSize="14">StaticResource</Button>
<Button x:Name="btn_ChangeResource" Click="btn_ChangeResource_Click" Margin="5" Padding="5" FontWeight="Bold" FontSize="14">ChangeResource</Button>
<Button Background="{DynamicResource TileBrush}" Margin="5" Padding="5" FontWeight="Bold" FontSize="14">DynamicResource</Button>
</StackPanel>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace ResourcesDemo
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void btn_ChangeResource_Click(object sender, RoutedEventArgs e)
{
//代码检索资源
//方法一:
//this.Resources["TileBrush"] = new SolidColorBrush(Colors.LightBlue);
//方法二:当找不到指定的资源时会抛异常
//btn_ChangeResource.Background = (ImageBrush)this.FindResource("TileBrush");
//方法三:当找不到指定的资源时不会抛异常
btn_ChangeResource.Background = (ImageBrush)this.TryFindResource("TileBrush");
btn_ChangeResource.Focusable = false; //为的是马上能看到效果
}
}
}