Silveright全局样式应用
在Web开发中要保持风格样式的一致性,在asp.net中做法是提取样式,写在一个CSS文件中,在web页面中添加对这个样式的引用,
在此要说的是Silverlight中做法,稍有些不同,
第一步,建立样式文件,并添加到Assets文件夹中,如图所示
第二步,在App.xaml文件中指定样式文件路径
1 <Application.Resources>
2 <ResourceDictionary>
3 <ResourceDictionary.MergedDictionaries>
4 <ResourceDictionary Source="Assets/Styles.xaml"/>
5 <ResourceDictionary Source="Assets/Brushes.xaml"/>
6 <ResourceDictionary Source="Assets/CoreStyles.xaml"/>
7 <ResourceDictionary Source="Assets/SDKStyles.xaml"/>
8 <ResourceDictionary Source="Assets/ToolkitStyles.xaml"/>
9 </ResourceDictionary.MergedDictionaries>
10 </ResourceDictionary>
11 </Application.Resources>
2 <ResourceDictionary>
3 <ResourceDictionary.MergedDictionaries>
4 <ResourceDictionary Source="Assets/Styles.xaml"/>
5 <ResourceDictionary Source="Assets/Brushes.xaml"/>
6 <ResourceDictionary Source="Assets/CoreStyles.xaml"/>
7 <ResourceDictionary Source="Assets/SDKStyles.xaml"/>
8 <ResourceDictionary Source="Assets/ToolkitStyles.xaml"/>
9 </ResourceDictionary.MergedDictionaries>
10 </ResourceDictionary>
11 </Application.Resources>
第三步,就可以在页面文件中引用所添加的样式啦,
<Border x:Name="contentBorder" Style="{StaticResource ContentBorderStyle}">
<TextBlock Text="真心真意过一生"/>
</Border>
<TextBlock Text="真心真意过一生"/>
</Border>
补充:
样式文件写法,以TextBlock控件为例:
先写要控件的属性然后是属性值,类似字典的方式,
1 <Style x:Key="TextBlockStyle" TargetType="TextBlock">
2 <Setter Property="Foreground" Value="{StaticResource BodyTextColorBrush}"/>
3 <Setter Property="FontFamily" Value="{StaticResource HeaderFontFamily}"/>
4 <Setter Property="FontSize" Value="{StaticResource ContentFontSize}"/>
5 <Setter Property="TextWrapping" Value="Wrap"/>
6 <Setter Property="Margin" Value="0,2,0,2"/>
7 <Setter Property="HorizontalAlignment" Value="Left"/>
8 <Setter Property="TextOptions.TextHintingMode" Value="Animated" />
9 </Style>
2 <Setter Property="Foreground" Value="{StaticResource BodyTextColorBrush}"/>
3 <Setter Property="FontFamily" Value="{StaticResource HeaderFontFamily}"/>
4 <Setter Property="FontSize" Value="{StaticResource ContentFontSize}"/>
5 <Setter Property="TextWrapping" Value="Wrap"/>
6 <Setter Property="Margin" Value="0,2,0,2"/>
7 <Setter Property="HorizontalAlignment" Value="Left"/>
8 <Setter Property="TextOptions.TextHintingMode" Value="Animated" />
9 </Style>
作者:逐帆
出处:http://www.cnblogs.com/langhua/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。