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 ...
随机推荐
- 2016 Multi-University Training Contest 1 F.PowMod
PowMod Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total Su ...
- H5移动前端开发常用高能css3汇总
1.禁止a标签点击高亮,这些都是非官方属性,但实用性超强 html,body{ -webkit-touch-callout: none; //禁止或显示系统默认菜单 -webkit-user-sele ...
- 我觉得好用的VS扩展(不定期更新)
首先向这些扩展的创作者致敬 这里都是2013版的 有些在给出的连接里有该扩展支持的其他版本连接 当然你也可以通过 VS->工具->扩展和更新->在线->搜索扩展名 来找到它们 ...
- Json.Net的简单使用
1.添加程序集 Newtonsoft.Json.dll 2.创建模型 public class Person { public string Name { get; set; } public ...
- Unity3D "Library\UnityAssemblies\UnityEngine.xml" is denied错误解决方法
错误信息 Access to the path "Library\UnityAssemblies\UnityEngine.xml" is denied 无法修改改文件 Unity版 ...
- cxf客户端动态调用空指针异常
异常信息如下: 二月 , :: 上午 org.apache.cxf.common.jaxb.JAXBUtils logGeneratedClassNames 信息: Created classes: ...
- 实战Java虚拟机之二“虚拟机的工作模式”
今天开始实战Java虚拟机之二:“虚拟机的工作模式”. 总计有5个系列 实战Java虚拟机之一“堆溢出处理” 实战Java虚拟机之二“虚拟机的工作模式” 实战Java虚拟机之三“G1的新生代GC” 实 ...
- 关于malloc函数的动态分配问题
malloc函数动态分配了一个整型的内存空间,让abc都指向刚申请的空间,所以只有最后一个赋值语句的值保留在了空间里 #include<stdio.h> main() { int *a,* ...
- ASP.NET State Server 服务 sessionState
在发布ASP.NET网站的时候,出现state server错误:Server Error in '/' Application.----------------------------------- ...
- PHP 常用的header头部定义汇总
<?phpheader('HTTP/1.1 200 OK'); // ok 正常访问header('HTTP/1.1 404 Not Found'); //通知浏览器 页面不存在header(' ...