Windows Phone 三、样式和资源

定义样式和引用资源

 1     <Page.Resources>
 2         <!-- 向资源字典中添加一个键为ButtonBackground值为SolidColorBrush对象 -->
 3         <SolidColorBrush
 4             x:Key="ButtonBackground"
 5             Color="Aqua"/>
 6         <!-- 向资源字典中添加一个键为ButtonForeground值为SolidColorBrush对象 -->
 7         <SolidColorBrush
 8             x:Key="ButtonForeground"
 9             Color="Black"/>
10         <!-- 向资源字典中添加一个键为ButtonFontSize值为x:Double对象 -->
11         <x:Double x:Key="ButtonFontSize">20</x:Double>
12     </Page.Resources>
13     <Grid>
14         <!--根据资源名称,引用资源-->
15         <Button
16             Content="Button"
17             Background="{StaticResource ButtonBackground}"
18             Foreground="{StaticResource ButtonForeground}"
19             FontSize="{StaticResource ButtonFontSize}"/>
20     </Grid>

资源字典中可以添加各种各样类型的资源,这取决于资源对象的类型,不同对象的类型,对应不同类型的资源标签。

颜色对应SolidColorBrush  数值对应x:Double

类型选择器

 1     <Page.Resources>
 2         <!--类型选择器-->
 3         <!--Style节点可以不用指定一个具体的键,有一个默认的键(typeof(Button))-->
 4         <Style TargetType="Button">
 5             <!--默认样式-->
 6             <Setter Property="Width" Value="200"/>
 7             <Setter Property="Background" Value="HotPink"/>
 8         </Style>
 9         <Style x:Key="ButtonStyle" TargetType="Button">
10             <!--ButtonStyle样式-->
11             <Setter Property="Width" Value="300"/>
12             <!--在Value无法赋值的情况下,可以使用以下写法-->
13             <Setter Property="Background">
14                 <Setter.Value>
15                     <SolidColorBrush Color="Aqua"/>
16                 </Setter.Value>
17             </Setter>
18         </Style>
19         <!--演示x:Name也可以-->
20         <Style x:Name="ButtonName" TargetType="Button"/>
21     </Page.Resources>
22     <StackPanel>
23         <!--Button的Style默认指向的键为this.GetType()/typeof(Button)默认样式-->
24         <Button Content="Button1"/>
25         <!--指定ButtonStyle样式-->
26         <Button
27             Content="Button2"
28             Style="{StaticResource ButtonStyle}"/>
29     </StackPanel>

外部资源引用

1 <ResourceDictionary
2     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
4     <SolidColorBrush x:Key="ButtonBackground" Color="DarkOrchid"/>
5 </ResourceDictionary>
Styles.xaml

Styles.xaml 被创建在Resources文件夹当中

主程序资源

 1 <Application
 2     x:Class="MyApp.App"
 3     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 4     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 5     xmlns:local="using:MyApp">
 6     <!--Application.Resources全局共享-->
 7     <Application.Resources>
 8         <SolidColorBrush x:Key="ButtonBackground" Color="Navy"/>
 9     </Application.Resources>
10 </Application>

外部引用代码

 1 <Page
 2     x:Class="MyApp.MainPage"
 3     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 4     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 5     xmlns:local="using:MyApp"
 6     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
 7     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
 8     mc:Ignorable="d"
 9     Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
10     <!--Page.Resources整个页面共享-->
11     <Page.Resources>
12         <ResourceDictionary Source="Resources/Styles.xaml"/>
13     </Page.Resources>
14     <Grid>
15         <!--局部共享-->
16         <Grid.Resources>
17             <!--ResourceDictionary标签可省略-->
18             <ResourceDictionary>
19                 <!--就近原则-->
20                 <SolidColorBrush x:Key="ButtonBackground" Color="HotPink"/>
21             </ResourceDictionary>
22         </Grid.Resources>
23         <Button Content="Button"
24                 Background="{StaticResource ButtonBackground}"/>
25     </Grid>
26 </Page>

不同主题定义不同资源

 1     <Page.Resources>
 2         <!--为不同主题定义不同资源必须写ResourceDictionary标签-->
 3         <ResourceDictionary>
 4             <!--也是一个资源字典-->
 5             <ResourceDictionary.ThemeDictionaries>
 6                 <!--Default是固定值,默认缺省状态,很少使用,一般使用下面三种-->
 7                 <ResourceDictionary x:Key="Default">
 8                     <SolidColorBrush x:Key="Color" Color="Aqua"/>
 9                 </ResourceDictionary>
10                 <!--Dark是固定值,深色主题状态-->
11                 <ResourceDictionary x:Key="Dark">
12                     <SolidColorBrush x:Key="Color" Color="Red"/>
13                 </ResourceDictionary>
14                 <!--Light是固定值,浅色主题状态-->
15                 <ResourceDictionary x:Key="Light">
16                     <SolidColorBrush x:Key="Color" Color="Green"/>
17                 </ResourceDictionary>
18                 <!--HighContrast是固定值,高对比主题状态-->
19                 <ResourceDictionary x:Key="HighContrast">
20                     <SolidColorBrush x:Key="Color" Color="Blue"/>
21                 </ResourceDictionary>
22             </ResourceDictionary.ThemeDictionaries>
23         </ResourceDictionary>
24     </Page.Resources>
25     <StackPanel>
26         <!--ThemeResource可以实时的根据主题变化而选择不同资源,动态读取,不断侦测,消耗资源、性能、电量,效率低-->
27         <Button Background="{ThemeResource Color}" Content="ThemeResource"/>
28         <!--StaticResource应用启动时选择不同资源,用于引用静止不动的资源(控件模版)效率高-->
29         <Button Background="{StaticResource Color}" Content="StaticResource"/>
30     </StackPanel>
原文地址:https://www.cnblogs.com/includeling/p/4563704.html