WPF资源系统是一种保管一系列有用对象的简单方法,从而使您可以更容易重用这些对象,可以在代码中创建和操作资源,也可以在XAML标记中定义资源,一旦定义了资源,就可以在窗口标记的所有其他部分使用资源。
- 资源的集合
每个元素都有Resources属性,该属性存储了一个资源字典集合。资源集合可包含任意类型的对象,并根据字符串编写索引。WPF每个继承自FrameworkElement类都具有Resource属性,每个元素即可以访问自身的Resource对象,也可以访问父对象的Resource资源。资源的定义如下:
<Window x:Class="Example.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:Example" xmlns:albert="http://www.albert.com" mc:Ignorable="d" Title="MainWindow" Height="350" Width="525"> <Window.Resources> <ImageBrush x:Key="tileBrush" TileMode="Tile" ViewportUnits="Absolute" Viewport="0 0 10 10" ImageSource="12930B932320-5b61.png" Opacity="0.3"></ImageBrush> </Window.Resources> <Grid> <albert:ClassTest Background="{StaticResource tileBrush}"></albert:ClassTest> <albert:ClassMin></albert:ClassMin> </Grid> </Window>
Resource属性是一个资源字典集合(它是ResourceDictionary类的一个实例),资源是放在嵌套元素中,为指定资源,通过x:Key进行区分资源的名称,也就是ResourceKey属性。
- 资源的层次
每个元素都有自己的资源集合,为了找到期望的资源,WPF在元素树中进行递归搜索,我们的资源定以区域可分为多个查找主要查找层次。
系统资源:
系统资源是系统中已经预定的资源,如SystemColors和SystemFonts等
应用程序资源:
定义在App.xml中的资源,这个资源在整个应用程序中都可以使用,如果计划在多个窗体中使用相同的特性,那么这个资源将是一个很好的选择
<Application x:Class="Example.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:Example" StartupUri="MainWindow.xaml"> <Application.Resources> <ImageBrush x:Key="tileBrush" TileMode="Tile" ViewportUnits="Absolute" Viewport="0 0 10 10" ImageSource="12930B932320-5b61.png" Opacity="0.3"></ImageBrush> </Application.Resources> </Application>
控件资源:
定义在控件中的资源,可以在当前控件中和当前控件的子控件使用的 资源,其定义方式如下:
<Window x:Class="Example.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:Example" xmlns:albert="http://www.albert.com" mc:Ignorable="d" Title="MainWindow" Height="350" Width="525"> <Window.Resources> <ImageBrush x:Key="tileBrush1" TileMode="Tile" ViewportUnits="Absolute" Viewport="0 0 10 10" ImageSource="12930B932320-5b61.png" Opacity="0.3"></ImageBrush> </Window.Resources> <Grid> <Button> <Button.Resources> <ImageBrush x:Key="tileBrush2" TileMode="Tile" ViewportUnits="Absolute" Viewport="0 0 10 10" ImageSource="12930B932320-5b61.png" Opacity="0.3"></ImageBrush> </Button.Resources> </Button> </Grid> </Window>
3、资源字典
如何希望多个项目之间共享资源,可以创建资源字典。资源字典只有XAML文档,处理存储希望使用的资源以外,不做其他任何事情。只有在当前应用程序中添加资源字典,务必要将Blend Action设置为Pages,这样可以保证为了获取最佳性能而将资源字典编译为BAML。
下面是一个资源字典的示例:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:Example"> <ImageBrush x:Key="r1"></ImageBrush> </ResourceDictionary>
使用资源字典,需要将其合并到应用程序某些位置的资源集合中,列如,可在特定窗口中执行此操作,但通常将其合并到应用程序的资源集合中,如下所示:
<Application x:Class="Example.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:Example" StartupUri="MainWindow.xaml"> <Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="Dictionary1.xaml"></ResourceDictionary> <ResourceDictionary Source="Dictionary2.xaml"></ResourceDictionary> </ResourceDictionary.MergedDictionaries> <ImageBrush x:Key="tileBrush" TileMode="Tile" ViewportUnits="Absolute" Viewport="0 0 10 10" ImageSource="12930B932320-5b61.png" Opacity="0.3"></ImageBrush> </ResourceDictionary> </Application.Resources> </Application>