文件的語(yǔ)法格式如下
Test.xaml
<ResourceDictionaryxmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:chartingToolkit="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit"
xmlns:visualizationToolkit="clr-namespace:System.Windows.Controls.DataVisualization;assembly=System.Windows.Controls.DataVisualization.Toolkit">
<!--定義樣式資源-->
<Style x:Key="TextBlockStyle1" TargetType="TextBlock">
<Setter Property="Foreground" Value="Orange"/>
<Setter Property="FontSize" Value="24"/>
<Setter Property="VerticalAlignment" Value="Bottom"/>
</Style>
<!--定義數(shù)據(jù)模板資源-->
<DataTemplate x:Key="cityDetails">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="90"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<TextBlock Text="Activity: "
Grid.Column="0"
Grid.Row="0"
Style="{StaticResource detailsSmallTitle}"/>
<TextBlock Text="{Binding Activity}"
Grid.Column="1"
Grid.Row="0"
Style="{StaticResource detailsSmallText}"/>
……
</Grid>
</DataTemplate>
<!--定義控件模板資源-->
<ControlTemplate x:Key="ControlTemplateTest"
TargetType="chartingToolkit:Chart">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<visualizationToolkit:Title Grid.ColumnSpan="2"
Content="{TemplateBinding Title}"
Style="{TemplateBinding TitleStyle}"/>
……
</Grid>
</ControlTemplate>
</ResourceDictionary>
Style的x:Key屬性是資源字典里面的資源的唯一的標(biāo)示符,也是作為在其他頁(yè)面調(diào)用的一個(gè)唯一的Key來(lái)進(jìn)行調(diào)用。
2、調(diào)用資源資源中的資源
在MainPage.xaml頁(yè)面中添加資源字典,語(yǔ)法如下
<phone:PhoneApplicationPage.Resources><ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Test.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</phone:PhoneApplicationPage.Resources>
ResourceDictionary.MergedDictionaries 獲取 ResourceDictionary 字典的集合,這些字典構(gòu)成了合并字典中的各種資源字典。
如果想在程序啟動(dòng)時(shí)加載所有的資源,可以再App.xaml頁(yè)面上添加資源的加載,語(yǔ)法如下
<Applicationx:Class="DataVisualizationOnWindowsPhone.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone">