<Window.Resources>
<!--创建一个单色画刷-->
<SolidColorBrush x:Key="myBrush" Color="Orange" />
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<!--给按钮设置背景颜色-->
<Button Grid.Row="0" Margin="5" Height="50" Width="400" Content="我是按钮,背景颜色使用了资源里对象SolidColorBrush" Background="{StaticResource myBrush}" />
<!--给矩形填充颜色-->
<Rectangle Grid.Row="1" Margin="5" Width="200" Height="200" Fill="{StaticResource myBrush}" />
</Grid>
// 在App.xaml文件里
<Application.Resources>
<!--创建一个全局单色画刷-->
<SolidColorBrush x:Key="myGlobalBrush" Color="DodgerBlue" />
</Application.Resources>
// 另外一个窗体里也定义个窗体级资源
<Window.Resources>
<!--创建一个单色画刷-->
<SolidColorBrush x:Key="myBrush" Color="Orange" />
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<!--给按钮设置背景颜色-->
<Button Grid.Row="0" Margin="5" Height="50" Width="400" Content="我使用了局部窗体级资源里对象SolidColorBrush" Background="{StaticResource myBrush}" />
<!--给按钮设置背景颜色-->
<Button Grid.Row="1" Margin="5" Height="50" Width="400" Content="我使用了程序级资源里对象SolidColorBrush" Background="{StaticResource myGlobalBrush}" />
</Grid>
-是一种组织和管理资源的方式,允许将资源集中定义在一个位置,并在整个应用程序中重复使用。资源字典通常包含在XAML文件中,并使用ResourceDictionary元素进行定义。
// 这里字典资源
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<!--创建一个单色画刷-->
<SolidColorBrush x:Key="myResourceDictionaryBrush" Color="Red" />
<!--定义一个样式,样式背景颜色引用上面的画刷-->
<Style x:Key="MyResourceDictionaryStyle" TargetType="Button">
<Setter Property="Background" Value="{StaticResource myResourceDictionaryBrush}"/>
<Setter Property="FontSize" Value="16"/>
</Style>
</ResourceDictionary>
// 这里是窗体,注册字典资源
<Window.Resources>
<!--引入字典资源-->
<ResourceDictionary Source="SolidColorBrushResource.xaml"/>
</Window.Resources>
<Grid>
<!--给按钮设置背景颜色-->
<Button Margin="5" Height="50" Foreground="White" Width="400" Content="我背景颜色和字体大小使用了字典资源里的Style" Style="{StaticResource MyResourceDictionaryStyle}" />
</Grid>
<Grid>
<Button x:Name="myButton" Content="我使用了控件级资源" Margin="5" Height="50" Width="400" >
<Button.Resources>
<!--设置背景颜色和字体颜色,最后在设置个字体大小-->
<SolidColorBrush x:Key="myButtonBrush" Color="Blue" />
<Style x:Key="myButtonStyle" TargetType="Button">
<Setter Property="Background" Value="{StaticResource myButtonBrush}"/>
<Setter Property="Foreground" Value="White"/>
<Setter Property="FontSize" Value="16"/>
</Style>
</Button.Resources>
<Button.Style>
<StaticResource ResourceKey="myButtonStyle"/>
</Button.Style>
</Button>
</Grid>
<Window.Resources>
<!--设置背景颜色画刷-->
<SolidColorBrush x:Key="myButtonBlueBrush" Color="Blue" />
<SolidColorBrush x:Key="myButtonRedBrush" Color="Red" />
<SolidColorBrush x:Key="myButtonOrangeBrush" Color="Orange" />
<SolidColorBrush x:Key="myButtonYellowBrush" Color="Yellow" />
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<!--定于三个动态资源背景的按钮,可以切换背景颜色 DynamicResource标记-->
<Button x:Name="myButtonBlue" Grid.Row="0" Width="200" Height="50" Margin="5" Content="我是按钮蓝色" FontSize="16" Foreground="White" Background="{DynamicResource myButtonBlueBrush}" />
<Button x:Name="myButtonRed" Grid.Row="1" Width="200" Height="50" Margin="5" Content="我是按钮红色" FontSize="16" Foreground="White" Background="{DynamicResource myButtonRedBrush}" />
<Button x:Name="myButtonOrange" Grid.Row="2" Width="200" Height="50" Margin="5" Content="我是按钮橘色" FontSize="16" Foreground="White" Background="{DynamicResource myButtonOrangeBrush}" />
<!--定义个静态资源的按钮,用来点击切换动态按钮背景颜色 StaticResource标记-->
<Button Grid.Row="3" Width="200" Height="50" Margin="5" Content="点击我动态改变资源颜色" FontSize="16" Background="{StaticResource myButtonYellowBrush}" Click="Button_Click"/>
</Grid>
using System.Windows;
using System.Windows.Media;
namespace WpfResourceApp
{
/// <summary>
/// StaticDynamicResource.xaml 的交互逻辑
/// </summary>
public partial class StaticDynamicResource : Window
{
private static bool dynamicResourceStatus=false;
public StaticDynamicResource()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
//改变资源
//定义三个不同颜色的画刷
SolidColorBrush brushGreen = dynamicResourceStatus==true? new SolidColorBrush(Colors.Blue): new SolidColorBrush(Colors.Green);
SolidColorBrush brushIndigo = dynamicResourceStatus == true ? new SolidColorBrush(Colors.Red): new SolidColorBrush(Colors.Indigo);
SolidColorBrush brushViolet = dynamicResourceStatus == true ? new SolidColorBrush(Colors.Orange): new SolidColorBrush(Colors.Violet);
//改变资源
Application.Current.Dispatcher.Invoke(() =>
{
this.myButtonBlue.Resources["myButtonBlueBrush"] = brushGreen;
this.myButtonRed.Resources["myButtonRedBrush"] = brushIndigo;
this.myButtonOrange.Resources["myButtonOrangeBrush"] = brushViolet;
});
dynamicResourceStatus=!dynamicResourceStatus;
}
}
}
公众号“点滴分享技术猿”