如何使用和定义字典资源(ResourceDictionary)

1、首先需要创建一个资源字典的文件,也就是一个xaml的文件。

文件的语法格式如下:Design.xaml

 1 <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 2                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 3                     xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"
 4                     xmlns:system="clr-namespace:System;assembly=mscorlib"
 5                     xmlns:controls="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls"
 6                     xmlns:controlsPrimitives="clr-namespace:Microsoft.Phone.Controls.Primitives;assembly=Microsoft.Phone.Controls">
 7 
 8     <!--如果想预览界面效果,把一下代码加到页面里。不加的话在运行时匹配.-->
 9     <!--<phone:PhoneApplicationPage.Resources>
10         <ResourceDictionary>
11             <ResourceDictionary.MergedDictionaries>
12                 <ResourceDictionary Source="../Design/Design.xaml"></ResourceDictionary>
13             </ResourceDictionary.MergedDictionaries>
14         </ResourceDictionary>
15     </phone:PhoneApplicationPage.Resources>-->
16 
17     <!--(主)字体颜色-->
18     <SolidColorBrush x:Key="Host_Color" Color="Black"/>
19 
20     <!--(所有页面)正在加载提示-->
21     <Style TargetType="TextBlock" x:Key="LoadingTextBlock">
22         <Setter Property="FontSize" Value="25"/>
23         <Setter Property="Text" Value="正在加载"/>
24         <Setter Property="Foreground" Value="#FF476FBF"/>
25     </Style>
26     <Style TargetType="ProgressBar" x:Key="LoadingProgressBar">
27         <Setter Property="IsIndeterminate" Value="True"/>
28         <Setter Property="Width" Value="320"/>
29         <Setter Property="Margin" Value="-20,0,0,-15"/>
30         <Setter Property="Foreground" Value="#FF476FBF"/>
31     </Style>
32 
33     <!--定义样式资源-->
34     <Style TargetType="TextBlock">
35         <Setter Property="Foreground" Value="{StaticResource Host_Color}"/>
36         <Setter Property="VerticalAlignment" Value="Center"/>
37         <Setter Property="HorizontalAlignment" Value="Center"/>
38     </Style>
39 
40 </ResourceDictionary>

Style的x:Key属性是资源字典里面的资源的唯一的标示符,也是作为在其他页面调用的一个唯一的Key来进行调用。

2、调用资源资源中的资源

在MainPage.xaml页面中添加资源字典,语法如下

1 <phone:PhoneApplicationPage.Resources>
2      <ResourceDictionary>
3           <ResourceDictionary.MergedDictionaries>
4                 <ResourceDictionary Source="Design.xaml"/>
5           </ResourceDictionary.MergedDictionaries>
6       </ResourceDictionary>
7  </phone:PhoneApplicationPage.Resources>

注意:Source的路径是指当前使用Design.xaml文件的页面,相对Design.xaml的位置;例如
----文件夹View

      ----文件Page.xaml

----Design.xaml

如果文件Page.xaml要使用Design.xaml,Source="../Design.xaml"

ResourceDictionary.MergedDictionaries   获取 ResourceDictionary 字典的集合,这些字典构成了合并字典中的各种资源字典。 

如果想在程序启动时加载所有的资源,可以再App.xaml页面上添加资源的加载,语法如下

 1 <Application
 2     x:Class="DataVisualizationOnWindowsPhone.App"
 3     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 4     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 5     xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone">
 6 
 7 
 8 
 9     <Application.Resources>
10         <!-- 添加资源 -->
11         <ResourceDictionary>
12             <ResourceDictionary.MergedDictionaries>
13                 <ResourceDictionary Source="Design.xaml"/>
14             </ResourceDictionary.MergedDictionaries>
15         </ResourceDictionary>
16     </Application.Resources>
17 
18 ……
19 
20 </Application>

3、使用字典资源中的资源

在MainPage.xaml页面中的控件调用自定义的资源,语法如下

调用字典资源中x:Key值为LoadingTextBlock的样式资源

1 <TextBlock Text="Some Text" Style="{StaticResource LoadingTextBlock}"/>

在cs页面调用字典资源,语法如下

1 ControlTemplate template;
2 template = Application.Current.Resources["ControlTemplateL"] as ControlTemplate;
3 MyCtrl.Template = template;

参考

MSDN的Silverlight资源字典详细介绍

http://msdn.microsoft.com/zh-cn/library/cc903952(v=VS.95).aspx

 

转载于:https://www.cnblogs.com/qq278360339/archive/2012/06/27/2565454.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值