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 ...
随机推荐
- Lua字符串库
1. 基础字符串函数: 字符串库中有一些函数非常简单,如: 1). string.len(s) 返回字符串s的长度: 2). string.rep(s,n) 返回字符串s重复n次的结 ...
- ubuntu快捷复制粘贴
今天使用putty,纠结复制粘贴的时候,才发现 原来只要选中文本后,就可以中键粘贴 整个桌面环境可用,新技能啊以前居然不知道
- HTML基础篇之图像热区补充一下图片相对地址的定义
HTML5课堂笔记理解2 上次说到图片相对地址的定义,举例了4中情形的下的不同目录的图片书写方法补充一个如果你要的图片目录跟上面四种都不一样话可以用以下属性值尝试 ./ 当前目录 ...
- 使用staruml学习画类图
//这是startuml 把uml 转换成的java代码: public class Circle implements Ishape { private double _radius; public ...
- Rails--n+1查询
listings = Listing.includes(:property).where(id: ids)
- 然并卵,腾讯QQ认证空间又再次关闭申请
昨天发布的腾讯QQ认证空间又开放申请的消息,此消息一放出,大家都去关注认证的事情,而马浩周发现在4月27日下午4-5点,腾讯QQ空间认证又再次关闭页面开放申请的通知,变成了以前停止审核的通知了. 可能 ...
- centos6修改nameserver
1.直接修改/etc/resolv.conf,重启网卡 #service network restart 后发现并没有修改掉 2.直接修改ifcfg-eth0文件 /etc/sysconfig/net ...
- 【转】C# GET 和 SET作用
http://www.cnblogs.com/yinxiangpei/articles/2357091.html C#中get和SET,看来看去还是看不懂,通俗一点解释一下,用了有什么好处,不用会怎么 ...
- How to use wget ?
1.How to get conent (not download page) of website? wget <websit> -q -O -
- Web测试的常用测试用例与知识
1. Web测试中关于登录的测试 2. 搜索功能测试用例设计 3. 翻页功能测试用例 4. 输入框的测试 5. Web测试的常用的检查点 6. 用户及权限管理功能常规测试方法 7. Web测试之兼容性 ...