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




使用字典资源一般有三个方面的内容。
1、首先需要创建一个资源字典的文件,也就是一个xaml的文件。

文件的语法格式如下

Test.xaml

<pre name="code" class="html"><ResourceDictionary
<span style="white-space:pre">	</span>xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
<span style="white-space:pre">	</span>xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
<span style="white-space:pre">	</span>xmlns:chartingToolkit="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Win<span style="white-space:pre">		</span>dows.Controls.DataVisualization.Toolkit"
<span style="white-space:pre">	</span>xmlns:visualizationToolkit="clr-namespace:System.Windows.Controls.DataVisualization;assembly=System.Windows<span style="white-space:pre">		</span>.Controls.DataVisualization.Toolkit">



<span style="white-space:pre">	</span><!--定义样式资源-->
<span style="white-space:pre">	</span><Style x:Key="TextBlockStyle1" TargetType="TextBlock">
<span style="white-space:pre">		</span><Setter Property="Foreground" Value="Orange"/>
<span style="white-space:pre">		</span><Setter Property="FontSize" Value="24"/>
<span style="white-space:pre">		</span><Setter Property="VerticalAlignment" Value="Bottom"/>
<span style="white-space:pre">	</span></Style>

<span style="white-space:pre">	</span><!--定义数据模板资源-->
<span style="white-space:pre">	</span><DataTemplate x:Key="cityDetails">
<span style="white-space:pre">		</span><Grid>
<span style="white-space:pre">			</span><Grid.ColumnDefinitions>
<span style="white-space:pre">				</span><ColumnDefinition Width="Auto"/>
<span style="white-space:pre">				</span><ColumnDefinition Width="90"/>
<span style="white-space:pre">			</span></Grid.ColumnDefinitions>
<span style="white-space:pre">			</span><Grid.RowDefinitions>
<span style="white-space:pre">				</span><RowDefinition />
<span style="white-space:pre">				</span><RowDefinition />
<span style="white-space:pre">			</span></Grid.RowDefinitions>
<span style="white-space:pre">			</span><TextBlock Text="Activity: "
<span style="white-space:pre">				</span>   Grid.Column="0"
<span style="white-space:pre">				</span>   Grid.Row="0"
<span style="white-space:pre">				</span>   Style="{StaticResource detailsSmallTitle}"/>
<span style="white-space:pre">			</span><TextBlock Text="{Binding Activity}"
<span style="white-space:pre">				</span>   Grid.Column="1"
<span style="white-space:pre">				</span>   Grid.Row="0"
<span style="white-space:pre">			</span>  <span style="white-space:pre">	</span>   Style="{StaticResource detailsSmallText}"/>
<span style="white-space:pre">			</span>……

<span style="white-space:pre">		</span></Grid>
<span style="white-space:pre">	</span></DataTemplate>

<span style="white-space:pre">	</span><!--定义控件模板资源-->
<span style="white-space:pre">	</span><ControlTemplate x:Key="ControlTemplateTest"
<span style="white-space:pre">			</span> TargetType="chartingToolkit:Chart">
<span style="white-space:pre">		</span><Grid>
<span style="white-space:pre">			</span><Grid.ColumnDefinitions>
<span style="white-space:pre">				</span><ColumnDefinition Width="*"/>
<span style="white-space:pre">				</span><ColumnDefinition Width="Auto"/>
<span style="white-space:pre">			</span></Grid.ColumnDefinitions>
<span style="white-space:pre">			</span><Grid.RowDefinitions>
<span style="white-space:pre">				</span><RowDefinition Height="Auto"/>
<span style="white-space:pre">				</span><RowDefinition Height="*"/>
<span style="white-space:pre">			</span></Grid.RowDefinitions>
<span style="white-space:pre">			</span><visualizationToolkit:Title Grid.ColumnSpan="2"
<span style="white-space:pre">					</span>Content="{TemplateBinding Title}"
<span style="white-space:pre">					</span>Style="{TemplateBinding TitleStyle}"/>
<span style="white-space:pre">			</span>……

<span style="white-space:pre">		</span></Grid>
<span style="white-space:pre">	</span></ControlTemplate>
</ResourceDictionary>


 
 

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

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

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

<phone:PhoneApplicationPage.Resources>
<span style="white-space:pre">	</span><ResourceDictionary>
<span style="white-space:pre">		</span><ResourceDictionary.MergedDictionaries>
<span style="white-space:pre">			</span><ResourceDictionary Source="Test.xaml"/>
<span style="white-space:pre">		</span></ResourceDictionary.MergedDictionaries>
<span style="white-space:pre">	</span></ResourceDictionary>
</phone:PhoneApplicationPage.Resources>


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

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

<Application
<span style="white-space:pre">	</span>x:Class="DataVisualizationOnWindowsPhone.App"
<span style="white-space:pre">	</span>xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
<span style="white-space:pre">	</span>xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
<span style="white-space:pre">	</span>xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone">

<span style="white-space:pre">	</span><Application.Resources>
<span style="white-space:pre">	</span><!-- 添加资源 -->
<span style="white-space:pre">		</span><ResourceDictionary>
<span style="white-space:pre">			</span><ResourceDictionary.MergedDictionaries>
<span style="white-space:pre">				</span><ResourceDictionary Source="Test.xaml"/>
<span style="white-space:pre">			</span></ResourceDictionary.MergedDictionaries>
<span style="white-space:pre">		</span></ResourceDictionary>
<span style="white-space:pre">	</span></Application.Resources>

<span style="white-space:pre">	</span>……

</Application>


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

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

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

<StackPanel x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<span style="white-space:pre">	</span><TextBlock Text="Some Text" Style="{StaticResource TextBlockStyle1}"/>
</StackPanel>


调用字典资源中x:Key值为cityDetails的数据模板资源

<ContentControl ContentTemplate="{StaticResource cityDetails}"
<span style="white-space:pre">	</span>HorizontalAlignment="Left"
<span style="white-space:pre">	</span>x:Name="DetailsControl" Margin="0,0,0,5"/>


调用字典资源中x:Key值为ControlTemplateTest的控件模板资源

<charting:Chart x:Name="myChart"
<span style="white-space:pre">	</span>Style="{StaticResource PhoneChartStyle}"
<span style="white-space:pre">	</span>Template="{StaticResource ControlTemplateTest}">

<span style="white-space:pre">	</span>……

</charting:Chart>


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

ControlTemplate template;

template = Application.Current.Resources["ControlTemplateTest"] as ControlTemplate;

myChart.Template = template;


更多的介绍请参考MSDN的Silverlight资源字典介绍

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值