定义和使用字典资源(ResourceDictionary)
来源:互联网 发布:深圳软件产业基地招聘 编辑:程序博客网 时间:2024/05/18 01:05
使用字典资源一般有三个方面的内容。
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资源字典介绍
0 0
- 定义和使用字典资源(ResourceDictionary)
- UWP入门(六)-- ResourceDictionary 和 XAML 资源引用
- WPF使用ResourceDictionary
- jeecg 数据字典的定义和使用
- 动态加载资源文件(ResourceDictionary)
- WPF资源字典使用
- C#使用资源字典
- Android 关键资源的定义和使用
- .rc Windows资源文件定义和使用
- 使用数据库定义资源、角色和权限
- 定义资源与使用资源
- wpf使用资源字典组织资源
- WPF 列表选择,动态调用ResourceDictionary内资源
- WP7中怎样定义和使用资源(Resource)
- xx.rc windows资源文件的定义和使用
- Android 学习笔记4:资源文件的定义和使用
- 数据流和数据字典的定义
- Swift-数组和字典的定义
- match查询
- HDOJ 5443 The Water Problem(线段树区间最值)(长春网络赛)
- 物联网的媒介——java usb串口通信
- css定义背景图片不显示原因
- 基础二:service生命周期
- 定义和使用字典资源(ResourceDictionary)
- 正则表达式替换单词首字母大写
- 关于错误 reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'
- Android bitmap图片处理
- 理清android中values, values-v11, value-v14之间的关系
- 一个即时排名的demo
- Activity的四种launchMode
- Cocos2dx-jsb 中Node的onEnter过程分析(1)
- yum的工作原理以及如何建立yum仓库