文件的语法格式如下
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>