资源字典的意义在于:
1.高效,可以通过资源定义对象,并在标记的多个地方使用,这会精简代码
2.可维护性,通过资源使用低级的格式化细节,如字号,并将它们移到便于修改的中央位置。在XAML中创建资源相当于在代码中创建常亮
3.适应性,一旦某些特定信息与应用程序的其他部分分离开来,并放在资源中,那么可以动态的修改这些信息,如修改皮肤或者语言
资源字典的使用分为三步:
1.新建资源字典
新建的如下:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
</ResourceDictionary>
2.新建一些资源,如string,double,color,SolidColorBrush
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib">
<sys:String x:Key="key001">资源字典string</sys:String>
<Color x:Key="colorkey001">#FF3232</Color>
<SolidColorBrush x:Key="solidBrush001" Color="{StaticResource colorkey001}"/>
<SolidColorBrush x:Key="solidBrush002" Color="#FF707070"/>
<DrawingImage x:Key="DrawingImageKey001">
<DrawingImage.Drawing>
<DrawingGroup>
<DrawingGroup.Children>
<GeometryDrawing Brush="#FFFF0000" Pen="{x:Null}">
<GeometryDrawing.Geometry>
<PathGeometry FillRule="Nonzero" Figures="M21.8225803375244,1.41084265708923L1046.75561523438,1.41084265708923 1046.75561523438,1024 21.8225803375244,1.41084265708923z" />
</GeometryDrawing.Geometry>
</GeometryDrawing>
</DrawingGroup.Children>
</DrawingGroup>
</DrawingImage.Drawing>
</DrawingImage>
</ResourceDictionary>
<sys:String x:Key="key001">资源字典string</sys:String>定义了一个string字符串,
在 System空间下,可以使用xmlns:sys="clr-namespace:System;assembly=mscorlib"> 来引入命名空间,就跟using一样
3.绑定资源字典
有代码和xaml两种方式
代码:
public static void loadRd()
{
var brushes_rd = new ResourceDictionary()
{
Source = new Uri("pack://Application:,,,/testXml;component/dictionary.xaml")
};
Application.Current.Resources = new ResourceDictionary()
{
MergedDictionaries =
{
brushes_rd
}
};
}
Appllication 使用上面的 MergedDictionaries,可以添加多个资源字典
或者直接:
this.Resources= new ResourceDictionary()
{
Source = new Uri("pack://Application:,,,/testXml;component/dictionary.xaml")
};
在基本控件中都有resources这个属性,而且查询资源的时候,会先从本控件开始找,然后一层层向上,最后会到达application的资源,若还是找不到则会查到系统本地的资源
使用XAML的方式,一般都是在app.xml下绑定,如:
<Application x:Class="WpfApp2.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApp2"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Test.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
4.使用资源
使用也分xaml和代码两种:
xaml如下:
<Button Margin="121,30,107,230" Click="Button_Click" Background="{StaticResource solidBrush001}">
代码则可以用:
Public MainWindow()
{
InitializeComponent();
//使用资源里的字符串
MessageBox.Show(this.FindResource("key001") .ToString());
//使用资源中的solidBrush
this.Background=(SolidColorBrush)this.FindResource("solidBrush001");
//使用setResourceReference绑定,可以动态修改,如果dictionary发生改变,界面也随之改变
this.SetResourceReference(BackgroundProperty,”solidBrush002”);
//设置如DrawingImage资源
var drawing=(DrawingImage)this.FindResource("DrawingImageKey001");
Image image=new Image{Source = t};
button.Content=drawing;
}
一般是使用FindResource 或者 用SetResourceReference来进行使用
总结下使用资源字典的步骤:
1.新建并添加资源
2.绑定资源字典,有代码和xaml两种方式
3.使用字典,有代码和xaml两种方式