Windows Phone 三、样式和资源
定义样式和引用资源
<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 三、样式和资源的更多相关文章
- windows phone (13) 样式继承
原文:windows phone (13) 样式继承 在上一遍文章中已经介绍到可以在Resources集合中定义样式,我们也可以在一个样式上引用其他的样式,这就是继承的概念,使用方法是将引用的样式放置 ...
- windows phone 三种数据共享的方式(8)
原文:windows phone 三种数据共享的方式(8) 本节实现的内容是数据共享,实现的效果描述:首先是建立两个页面,当页面MainPage通过事件导航到页面SecondPage是,我们需要将Ma ...
- Windows常见窗口样式和控件风格
Windows常见窗口样式和控件风格 王佰营 徐丽红 一.窗口样式 WS_POPUP 弹出式窗口(不能与WS_CHILDWINDOW样式同时使用)WS_CHILDWINDOW 子窗口(不能与WS_PO ...
- Windows 8.1 Preview 开发资源汇总
Microsoft Build 2013开发者大会已经结束,从Session安排上看主要以Windows 8.1为主.我相信大家有已经或多或少的体验过Windows 8.1 Preview了,关于操作 ...
- windows 10最新版镜像资源下载 Win10 ISO下载教程
最近发现原创写的文章被无良爬走,而且变成了无图尬文,所以开头附上原文地址: http://www.cnblogs.com/xueyudlut/p/7497975.html -------------- ...
- 转载:Windows下三分钟搭建Shadowoscks服务器端
Windows下三分钟搭建Shadowoscks服务器端 之前在V2EX上有人问为啥没人做个在Windows上一键运行Shadowsocks服务器端的程序,我只想说……这是因为没人关注我的libQtS ...
- Android的三种主流资源尺寸
Android三种主流资源屏幕尺寸:QVGA.HVGA.WVGA VGA的分辨率是640x480像素 QVGA(Quarter VGA)就是320x240,即VGA分辨率的1/4 HVGA(Half ...
- mutex,thread(c++11 windows linux三种方式)
一 c++11 windows linux三种方式 //#include <stdio.h> //#include <stdlib.h> //#include <uni ...
- Vue音乐播放器(三):项目目录介绍,以及图标字体、公共样式等资源准备
我们所有的开发都是基于修改src下面的目录 里面的文件去做开发即可 stylus的使用是需要下载stylus-loader的包的 渲染效果 配置修改eslintrc.js 配置了webpack.bas ...
随机推荐
- linux install matlab2014a
https://pan.baidu.com/s/1qYJ9tNm#list/path=%2F下载镜像文件 2#开始安装,全程最好断网 sudo mkdir /media/matlab sudo mou ...
- MSSQL 死锁查询
SELECT SYS.DM_EXEC_REQUESTS.SESSION_ID,TEXT AS '执行SQL',CLIENT_NET_ADDRESS AS '请求IP',SYS.DM_EXEC_CONN ...
- ArcEngine奇怪异常:HRESULT:0x80040351
错误如图 根据该博客,http://blog.csdn.net/u011609113/article/details/51155116, 显示该错误为Duplicate Field Names wit ...
- C#中(int)、int.Parse()、int.TryParse()和Convert.ToInt32()的区别
转自:http://www.cnblogs.com/leolis/p/3968943.html 在编程过程中,数据转换是经常要用到的,C#中数据转换的方法很多,拿将目标对象转换为 整型(int)来讲, ...
- 复制文件的问题:使用FileInputStream和FileOutputStream实现文件复制
public class Test{ public static void main(String [] args) { Test t=new Test(); t.upload(); } public ...
- 对拍老是忘记的看这里:bat代码
需要写三个程序,makedata.exe 产生测试数据, program1.exe 是你要检测的程序,program2.exe 往往是一个正确但效率不高(暴力的居多)的程序. 代码很简单,稍作解释:l ...
- 【iCore3 双核心板_FPGA】实验二十七:基于SDRAM的TFT驱动器的设计
实验指导书及代码包下载: http://pan.baidu.com/s/1c2dZihE
- Canvas里绘制矩阵文字
效果如下 实现方法: [ [0,0,1,1,1,0,0], [0,1,1,0,1,1,0], [1,1,0,0,0,1,1], [1,1,0,0,0,1,1], [1,1,0,0,0,1,1], [1 ...
- mvc学习(二)
1.后台foreach 与 html的关系 <table border="1">@for (var i = 0; i < 10; i++){@Html.Raw(i ...
- Memcache,Redis,MongoDB(数据缓存系统)方案对比与分析
mongodb和memcached不是一个范畴内的东西.mongodb是文档型的非关系型数据库,其优势在于查询功能比较强大,能存储海量数据.mongodb和memcached不存在谁替换谁的问题. 和 ...