WPF资源字典

如果相同的资源可用于不同的应用程序,把资源放在一个资源字典中就比较有效。

新建一个资源字典文件Dictionary1.xaml

 1 <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 2                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
 3     
 4     
 5     <!--线性渐变画笔-->
 6     <LinearGradientBrush x:Key="CyanGradientBrush" StartPoint="0,0" EndPoint="0.3,1">
 7         <GradientStop Offset="0.0" Color="LightCyan"/>
 8         <GradientStop Offset="0.14" Color="Cyan"/>
 9         <GradientStop Offset="0.7" Color="DarkCyan"/>
10     </LinearGradientBrush>
11     
12     <Style x:Key="PinkButtonStyle" TargetType="Button">
13         <Setter Property="FontSize" Value="22"/>
14         <Setter Property="Foreground" Value="White"/>
15         <Setter Property="Background">
16             <Setter.Value>
17                 <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
18                     <GradientStop Offset="0.0" Color="Pink"/>
19                     <GradientStop Offset="0.3" Color="DeepPink"/>
20                     <GradientStop Offset="0.9" Color="DarkOrchid"/>
21                 </LinearGradientBrush>
22             </Setter.Value>
23         </Setter>
24     </Style>
25     <!--对于目标项目,需要引用这个库,并把资源字典添加到这个字典中。-->
26     <!--通过ResourceDictioinary的MergeDicitonaries属性,可以使用添进来的多个资源字典文件-->
27 </ResourceDictionary>

 


 对于目标项目,需要引用这个库,并把资源字典添加到这个字典中。通过ResourceDictionary的MergedDictionaries属性,可以使用添加进来 的多个资源字典文件。

 1 App.xaml
 2 
 3 <Application x:Class="WPF_Test.App"
 4              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 5              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 6              StartupUri="Resources_Test.xaml">
 7     <Application.Resources>
 8         <ResourceDictionary>
 9             <ResourceDictionary.MergedDictionaries>
10                 <ResourceDictionary Source="Dictionary1.xaml"/>
11             </ResourceDictionary.MergedDictionaries>
12         </ResourceDictionary>
13     </Application.Resources>
14 </Application>

 

  现在就可以像使用本地资源那样使用引用程序集中的资源了:

1 <Button Name="PinkButton" Width="300" Height="50" Style="{StaticResource PinkButtonStyle}" Content="Referenced Resource"/>

 

posted @ 2016-05-23 20:49  zhaotianff  阅读(344)  评论(0编辑  收藏  举报