定义样式和引用资源

     <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>

Windows Phone 三、样式和资源的更多相关文章

  1. windows phone (13) 样式继承

    原文:windows phone (13) 样式继承 在上一遍文章中已经介绍到可以在Resources集合中定义样式,我们也可以在一个样式上引用其他的样式,这就是继承的概念,使用方法是将引用的样式放置 ...

  2. windows phone 三种数据共享的方式(8)

    原文:windows phone 三种数据共享的方式(8) 本节实现的内容是数据共享,实现的效果描述:首先是建立两个页面,当页面MainPage通过事件导航到页面SecondPage是,我们需要将Ma ...

  3. Windows常见窗口样式和控件风格

    Windows常见窗口样式和控件风格 王佰营 徐丽红 一.窗口样式 WS_POPUP 弹出式窗口(不能与WS_CHILDWINDOW样式同时使用)WS_CHILDWINDOW 子窗口(不能与WS_PO ...

  4. Windows 8.1 Preview 开发资源汇总

    Microsoft Build 2013开发者大会已经结束,从Session安排上看主要以Windows 8.1为主.我相信大家有已经或多或少的体验过Windows 8.1 Preview了,关于操作 ...

  5. windows 10最新版镜像资源下载 Win10 ISO下载教程

    最近发现原创写的文章被无良爬走,而且变成了无图尬文,所以开头附上原文地址: http://www.cnblogs.com/xueyudlut/p/7497975.html -------------- ...

  6. 转载:Windows下三分钟搭建Shadowoscks服务器端

    Windows下三分钟搭建Shadowoscks服务器端 之前在V2EX上有人问为啥没人做个在Windows上一键运行Shadowsocks服务器端的程序,我只想说……这是因为没人关注我的libQtS ...

  7. Android的三种主流资源尺寸

    Android三种主流资源屏幕尺寸:QVGA.HVGA.WVGA VGA的分辨率是640x480像素 QVGA(Quarter VGA)就是320x240,即VGA分辨率的1/4 HVGA(Half ...

  8. mutex,thread(c++11 windows linux三种方式)

    一 c++11  windows linux三种方式 //#include <stdio.h> //#include <stdlib.h> //#include <uni ...

  9. Vue音乐播放器(三):项目目录介绍,以及图标字体、公共样式等资源准备

    我们所有的开发都是基于修改src下面的目录 里面的文件去做开发即可 stylus的使用是需要下载stylus-loader的包的 渲染效果 配置修改eslintrc.js 配置了webpack.bas ...

随机推荐

  1. [转]passport.js学习笔记

    概述 passport.js是Nodejs中的一个做登录验证的中间件,极其灵活和模块化,并且可与Express.Sails等Web框架无缝集成.Passport功能单一,即只能做登录验证,但非常强大, ...

  2. Ext3文件系统mount选项和文件属性介绍

    mount选项 设置方式 ext3 mount选项可以通过多个方式进行设置:1)内核编译时: 内核menuconfig通过CONFIG_EXT3_DEFAULTS_TO_ORDERED编译控制选项,来 ...

  3. 如何更好地学习dubbo源代码(转)

    很荣幸,作为这样一款业界使用率和好评率出众的RPC框架的维护者,今天这个文章主要是想帮助那些热爱开源的同学,更好的来研究dubbo的源代码. 一.Dubbo整体架构 1.Dubbo与Spring的整合 ...

  4. SecureCRT中python脚本编写

    SecureCRT中python脚本编写学习指南 SecureCRT python 引言 在测试网络设备中,通常使用脚本对设备端进行配置和测试以及维护:对于PE设备的测试维护人员来说使用较多是Secu ...

  5. JavaScript第一天

    1.静态的网页技术和动态的网页技术 静态网页是相对于动态网页而言,是指没有后台数据库.不含程序和不可交互的网页.你编的是什么它显示的就是什么.不会有任何改变.静态网页相对更新起来比较麻烦,适用于一般更 ...

  6. TranslateAnimation参数

    看TranslateAnimation动画参数,一直忘记四个参数意思: public    TranslateAnimation(float fromXDelta, float toXDelta, f ...

  7. MySQL sql_safe_updates 分析

    我在练习MySQL操作语句时,使用一条完全没有错误的语句: update students set name='drake' where name='chuan'; 却报了如下错误: Error Co ...

  8. JS base64 加密和 后台 base64解密(防止中文乱码)

    直接上代码 1,js(2个文件,网上找的)  不要觉的长,直接复制下来就OK //UnicodeAnsi.js文件 //把Unicode转成Ansi和把Ansi转换成Unicode function ...

  9. C++ 画星号图形——空心三角形(星号居中对齐)(核心代码介绍)

    //输出另外一种由星号组成的三角形(星号居中对齐) int a;//控制组成三角形的星号的行数 cout<<"请输入要组成三解形的星号的行数n(n>=2):\n" ...

  10. Angular2对待一个URL的处理流程

    直到一年前我还不会想到我会写一本书出来,直到两三年前我还不会想到会把自己的业余时间放在写作(技术博客)上.当初在机缘巧合之下,我只是想把自己工作中遇到的问题总结起来,发布在博客中以供日后查阅.不想竟然 ...