Windows Phone 三、样式和资源

时间:2023-03-08 21:41:17

定义样式和引用资源

     <Page.Resources>
<!-- 向资源字典中添加一个键为ButtonBackground值为SolidColorBrush对象 -->
<SolidColorBrush
x:Key="ButtonBackground"
Color="Aqua"/>
<!-- 向资源字典中添加一个键为ButtonForeground值为SolidColorBrush对象 -->
<SolidColorBrush
x:Key="ButtonForeground"
Color="Black"/>
<!-- 向资源字典中添加一个键为ButtonFontSize值为x:Double对象 -->
<x:Double x:Key="ButtonFontSize">20</x:Double>
</Page.Resources>
<Grid>
<!--根据资源名称,引用资源-->
<Button
Content="Button"
Background="{StaticResource ButtonBackground}"
Foreground="{StaticResource ButtonForeground}"
FontSize="{StaticResource ButtonFontSize}"/>
</Grid>

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

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

类型选择器

     <Page.Resources>
<!--类型选择器-->
<!--Style节点可以不用指定一个具体的键,有一个默认的键(typeof(Button))-->
<Style TargetType="Button">
<!--默认样式-->
<Setter Property="Width" Value="200"/>
<Setter Property="Background" Value="HotPink"/>
</Style>
<Style x:Key="ButtonStyle" TargetType="Button">
<!--ButtonStyle样式-->
<Setter Property="Width" Value="300"/>
<!--在Value无法赋值的情况下,可以使用以下写法-->
<Setter Property="Background">
<Setter.Value>
<SolidColorBrush Color="Aqua"/>
</Setter.Value>
</Setter>
</Style>
<!--演示x:Name也可以-->
<Style x:Name="ButtonName" TargetType="Button"/>
</Page.Resources>
<StackPanel>
<!--Button的Style默认指向的键为this.GetType()/typeof(Button)默认样式-->
<Button Content="Button1"/>
<!--指定ButtonStyle样式-->
<Button
Content="Button2"
Style="{StaticResource ButtonStyle}"/>
</StackPanel>

外部资源引用

 <ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<SolidColorBrush x:Key="ButtonBackground" Color="DarkOrchid"/>
</ResourceDictionary>

Styles.xaml

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

主程序资源

 <Application
x:Class="MyApp.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:MyApp">
<!--Application.Resources全局共享-->
<Application.Resources>
<SolidColorBrush x:Key="ButtonBackground" Color="Navy"/>
</Application.Resources>
</Application>

外部引用代码

 <Page
x:Class="MyApp.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:MyApp"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<!--Page.Resources整个页面共享-->
<Page.Resources>
<ResourceDictionary Source="Resources/Styles.xaml"/>
</Page.Resources>
<Grid>
<!--局部共享-->
<Grid.Resources>
<!--ResourceDictionary标签可省略-->
<ResourceDictionary>
<!--就近原则-->
<SolidColorBrush x:Key="ButtonBackground" Color="HotPink"/>
</ResourceDictionary>
</Grid.Resources>
<Button Content="Button"
Background="{StaticResource ButtonBackground}"/>
</Grid>
</Page>

不同主题定义不同资源

     <Page.Resources>
<!--为不同主题定义不同资源必须写ResourceDictionary标签-->
<ResourceDictionary>
<!--也是一个资源字典-->
<ResourceDictionary.ThemeDictionaries>
<!--Default是固定值,默认缺省状态,很少使用,一般使用下面三种-->
<ResourceDictionary x:Key="Default">
<SolidColorBrush x:Key="Color" Color="Aqua"/>
</ResourceDictionary>
<!--Dark是固定值,深色主题状态-->
<ResourceDictionary x:Key="Dark">
<SolidColorBrush x:Key="Color" Color="Red"/>
</ResourceDictionary>
<!--Light是固定值,浅色主题状态-->
<ResourceDictionary x:Key="Light">
<SolidColorBrush x:Key="Color" Color="Green"/>
</ResourceDictionary>
<!--HighContrast是固定值,高对比主题状态-->
<ResourceDictionary x:Key="HighContrast">
<SolidColorBrush x:Key="Color" Color="Blue"/>
</ResourceDictionary>
</ResourceDictionary.ThemeDictionaries>
</ResourceDictionary>
</Page.Resources>
<StackPanel>
<!--ThemeResource可以实时的根据主题变化而选择不同资源,动态读取,不断侦测,消耗资源、性能、电量,效率低-->
<Button Background="{ThemeResource Color}" Content="ThemeResource"/>
<!--StaticResource应用启动时选择不同资源,用于引用静止不动的资源(控件模版)效率高-->
<Button Background="{StaticResource Color}" Content="StaticResource"/>
</StackPanel>