StaticResource

  • ThemeResource

示例
1、演示“StaticResource”相关知识点
Resource/StaticResourceDemo.xaml

<Page
x:Class="Windows10.Resource.StaticResourceDemo"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Windows10.Resource"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" xmlns:common="using:Windows10.Common"> <!--
定义各种类型的 StaticResource(Resources 属性来自 FrameworkElement)
系统会在加载 xaml 的时候去查找并应用 StaticResource 指定的资源
StaticResource 需要先定义后引用,比如如果把下面这段 Page.Resources 代码放到 Grid 后面去定义,那么就会报错
-->
<Page.Resources>
<x:String x:Key="MyString">StaticResource 经常用于指定 Style 或 ControlTemplate 资源,参见 /Controls/UI 部分</x:String>
<x:Double x:Key="MyDouble1">24</x:Double>
<x:Double x:Key="MyDouble2">48</x:Double>
<Thickness x:Key="MyThickness">20,20,20,20</Thickness>
<common:Employee x:Key="CurrentEmployee" Name="wanglei" Age="35" IsMale="True"></common:Employee> <!--静态资源也可以用来定义控件-->
<Flyout x:Key="MyFlyout">
<StackPanel>
<TextBlock Text="我是 Flyout 中的内容" />
</StackPanel>
</Flyout>
</Page.Resources> <Grid Background="Transparent">
<StackPanel Margin="10 0 10 10"> <!--
下面演示 StaticResource 的使用方法
--> <!--
StaticResource 的 3 种引用方式
-->
<TextBlock Name="textBlock0" Margin="5" Text="{StaticResource MyString}" />
<TextBlock Name="textBlock1" Margin="5" Text="{StaticResource ResourceKey=MyString}" />
<TextBlock Name="textBlock2" Margin="5">
<TextBlock.Text>
<StaticResource ResourceKey="MyString" />
</TextBlock.Text>
</TextBlock> <TextBlock Name="textBlock3" Margin="5" FontSize="{StaticResource MyDouble1}" Text="我是 TextBlock">
<!--
Style 或 ControlTemplate 内都可以引用静态资源
-->
<TextBlock.Style>
<Style TargetType="TextBlock">
<Setter Property="Padding" Value="{StaticResource MyThickness}" />
</Style>
</TextBlock.Style>
</TextBlock> <!--
动态改变引用的资源
-->
<Button Name="btnChangeStaticResource" Content="改变引用的 StaticResource" Margin="5" Click="btnChangeStaticResource_Click" /> <!--
设置 FrameworkElement 的 DataContext 为一个指定的静态资源
-->
<TextBlock Margin="5" DataContext="{StaticResource CurrentEmployee}" Text="{Binding Name}" />
<TextBlock Margin="5" DataContext="{StaticResource CurrentEmployee}" Text="{Binding Age}" />
<TextBlock Margin="5" DataContext="{StaticResource CurrentEmployee}" Text="{Binding IsMale}" /> <!--
引用指定的静态资源(此静态资源是一个控件)
-->
<Button Margin="5" Content="按我弹出 Flyout" Flyout="{StaticResource MyFlyout}" /> <!--
如果一个 FrameworkElement 要引用内部 Resources 的话,像下面这么写是会报错的,因为资源被先引用后定义了
-->
<!--
<TextBlock Margin="5" Text="我是 TextBlock" Foreground="{StaticResource MyTextBlockForeground}">
<TextBlock.Resources>
<SolidColorBrush x:Key="MyTextBlockForeground" Color="Red" />
</TextBlock.Resources>
</TextBlock>
--> <!--
如果一个 FrameworkElement 要引用内部 Resources 的话,需要像下面这么写(资源先定义后引用)
-->
<TextBlock Margin="5" Text="我是 TextBlock">
<TextBlock.Resources>
<SolidColorBrush x:Key="MyTextBlockForeground" Color="Red" />
</TextBlock.Resources>
<TextBlock.Foreground>
<StaticResource ResourceKey="MyTextBlockForeground" />
</TextBlock.Foreground>
</TextBlock> </StackPanel>
</Grid>
</Page>

Resource/StaticResourceDemo.xaml.cs

/*
* 演示“StaticResource”相关知识点
*/ using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls; namespace Windows10.Resource
{
public sealed partial class StaticResourceDemo : Page
{
public StaticResourceDemo()
{
this.InitializeComponent();
} private void btnChangeStaticResource_Click(object sender, RoutedEventArgs e)
{
// 获取 Application 中的资源
// (double)Application.Current.Resources["MyDouble1"]; // 获取关联 xaml 内的资源(本例中的资源定义在 xaml 中的 Page 下,所以用 this.Resources[""] 来获取)
if (textBlock3.FontSize == (double)this.Resources["MyDouble1"])
{
// 引用指定的资源
textBlock3.FontSize = (double)this.Resources["MyDouble2"];
}
else
{
textBlock3.FontSize = (double)this.Resources["MyDouble1"];
}
}
}
}

2、演示“ThemeResource”相关知识点
Resource/ThemeResourceDemo.xaml

<Page
x:Class="Windows10.Resource.ThemeResourceDemo"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Windows10.Resource"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"> <!--
ThemeResource 与 StaticResource 的区别是:ThemeResource 在运行时会根据主题的变化而重新计算
--> <!--
默认的主题资源的相关定义在如下位置(以我的开发环境为例)
1、C:\Program Files (x86)\Windows Kits\10\DesignTime\CommonConfiguration\Neutral\UAP\10.0.10586.0\Generic\generic.xaml
2、C:\Program Files (x86)\Windows Kits\10\DesignTime\CommonConfiguration\Neutral\UAP\10.0.10586.0\Generic\themeresources.xaml 注:默认的主题资源不会复制到应用程序中,这些资源字典在内存中作为 Windows 运行时本身的一部分存在
--> <Page.Resources> <ResourceDictionary>
<!--
通过 ResourceDictionary 内的 ResourceDictionary.ThemeDictionaries 内的 ResourceDictionary 来定义不同主题的资源
在资源中定义的主题分为 3 种:"Light", "Dark" 和 "HighContrast",其中 High Contrast(高对比度模式) 不常用,就不详细介绍了
-->
<ResourceDictionary.ThemeDictionaries> <!--
Default 主题,对应 ElementTheme.Dark 或 ApplicationTheme.Dark
-->
<ResourceDictionary x:Key="Default">
<!--
这里摘自 themeresources.xaml 中的部分定义,如果要覆盖其中的定义就自己再定义同名资源即可
-->
<SolidColorBrush x:Key="ApplicationPageBackgroundThemeBrush" Color="#FF000000" />
<SolidColorBrush x:Key="SystemControlBackgroundAccentBrush" Color="{ThemeResource SystemAccentColor}" /> <!--
这是系统级资源,不在 themeresources.xaml 内,其含义是在“设置”->“个性化”->“颜色”中选择的主题色,当然也可以这样重新定义
-->
<Color x:Key="SystemAccentColor">#FFFF0000</Color>
</ResourceDictionary> <!--
HighContrast 主题,不常用,就不详细介绍了
-->
<ResourceDictionary x:Key="HighContrast">
<!--
这里摘自 themeresources.xaml 中的部分定义,其引用的一些颜色资源来自系统级,比如 SystemColorWindowColor 或 SystemColorButtonFaceColor 之类的,他们不在 themeresources.xaml 内
比如在“设置”->“轻松使用”->“高对比度”中目前可以设置 4 中不同的高对比度主题,每一种对应的颜色资源都不一样
-->
<SolidColorBrush x:Key="ApplicationPageBackgroundThemeBrush" Color="{ThemeResource SystemColorWindowColor}" />
<SolidColorBrush x:Key="SystemControlBackgroundAccentBrush" Color="{ThemeResource SystemColorButtonFaceColor}" />
</ResourceDictionary> <!--
Light 主题,对应 ElementTheme.Light 或 ApplicationTheme.Light
-->
<ResourceDictionary x:Key="Light">
<!--
这里摘自 themeresources.xaml 中的部分定义,如果要覆盖其中的定义就自己再定义同名资源即可
-->
<SolidColorBrush x:Key="ApplicationPageBackgroundThemeBrush" Color="#FFFFFFFF" />
<SolidColorBrush x:Key="SystemControlBackgroundAccentBrush" Color="{ThemeResource SystemAccentColor}" /> <!--
这是系统级资源,不在 themeresources.xaml 内,其含义是在“设置”->“个性化”->“颜色”中选择的主题色,当然也可以这样重新定义
-->
<Color x:Key="SystemAccentColor">#FF00FF00</Color>
</ResourceDictionary> </ResourceDictionary.ThemeDictionaries>
</ResourceDictionary> </Page.Resources> <Grid Background="Transparent"> <StackPanel Name="panel" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" Margin="10 0 10 10"> <TextBlock Margin="5" Name="lblMsg" Foreground="Blue" /> <TextBlock Margin="5" Text="Microsoft HoloLens全息眼镜由Microsoft 公司于北京时间2015年1月22日凌晨与Window10同时发布。" Foreground="Blue" /> <StackPanel Width="200" Height="100" Margin="5" HorizontalAlignment="Left" Background="{ThemeResource SystemControlBackgroundAccentBrush}" /> <!--动态变换主题,引用的主题资源会重新计算-->
<Button Name="btnChangeTheme" Click="btnChangeTheme_Click" Margin="5">变换主题</Button> </StackPanel> </Grid>
</Page>

Resource/ThemeResourceDemo.xaml.cs

/*
* 演示“ThemeResource”相关知识点
*
*
* 1、主题共有两种类别:Light 和 Dark,子会继承父的主题类别
* 2、Application 级别指定 Theme 的话,在 App.xaml 中做如下声明 <Application RequestedTheme="Dark"></Application>
* 3、FrameworkElement 级别指定 Theme 的话,则指定 FrameworkElement.RequestedTheme 即可
*
*
* Application.Current.RequestedTheme - 获取或设置 Application 级别的主题(ApplicationTheme 枚举:Light, Dark)
* FrameworkElement.RequestedTheme - 获取或设置 FrameworkElement 级别的主题(ElementTheme 枚举:Default, Light, Dark)
* 注:ElementTheme 比 ApplicationTheme 多了一个 Default,其含义是当 ElementTheme 为 Default 时,其实际主题为 application 级主题
*/ using System;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls; namespace Windows10.Resource
{
public sealed partial class ThemeResourceDemo : Page
{
public ThemeResourceDemo()
{
this.InitializeComponent(); DisplayMessage();
} private void DisplayMessage()
{
// 当前 Application 级别的 Theme
lblMsg.Text = "application theme: " + Application.Current.RequestedTheme.ToString();
lblMsg.Text += Environment.NewLine; // 当前 panel 的 Theme
lblMsg.Text += "FrameworkElement theme: " + panel.RequestedTheme.ToString();
} // 动态变换主题,引用的主题资源会重新计算
private void btnChangeTheme_Click(object sender, RoutedEventArgs e)
{
if (panel.RequestedTheme == ElementTheme.Default) // 未指定 panel 的主题,则 panel 主题同 application 级主题
{
if (Application.Current.RequestedTheme == ApplicationTheme.Dark) // application 是 Dark 主题
{
panel.RequestedTheme = ElementTheme.Light;
}
else
{
panel.RequestedTheme = ElementTheme.Dark;
}
}
else if (panel.RequestedTheme == ElementTheme.Dark) // panel 是 Dark 主题
{
panel.RequestedTheme = ElementTheme.Light;
}
else // panel 是 Light 主题
{
panel.RequestedTheme = ElementTheme.Dark;
} DisplayMessage();
}
}
}

App.xaml

<Application
x:Class="Windows10.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Windows10" RequestedTheme="Dark">
<!--
上面的 RequestedTheme 被我设置为 Dark 了,关于主题的相关知识点请参见:/Resource/ThemeResourceDemo.xaml
--> </Application>

资源: StaticResource, ThemeResource的更多相关文章

  1. 背水一战 Windows 10 (10) - 资源: StaticResource, ThemeResource

    [源码下载] 背水一战 Windows 10 (10) - 资源: StaticResource, ThemeResource 作者:webabcd 介绍背水一战 Windows 10 之 资源 St ...

  2. [WPF]静态资源(StaticResource)和动态资源(DynamicResource)

    一.文章概述 本演示介绍WPF基本采用静态和动态的资源.而且两者都做一个简单的比较. 静态资源(StaticResource)指的是在程序加载内存时对资源的一次性使用,之后就不再訪问这个资源了:动态资 ...

  3. WPFの静态资源(StaticResource)和动态资源(DynamicResource)

    下面是前台代码: <Window.Resources>        <TextBlock x:Key="res1" Text="好好学习"/ ...

  4. 静态资源(StaticResource)和动态资源(DynamicResource)

    一.文章概述 本演示介绍了WPF的静态资源和动态资源的基本使用,并对两者做了简单的比较. 二.定义并使用资源 <Window x:Class="Demo010.MainWindow&q ...

  5. WPF学习之资源-Resources

    WPF学习之资源-Resources WPF通过资源来保存一些可以被重复利用的样式,对象定义以及一些传统的资源如二进制数据,图片等等,而在其支持上也更能体现出这些资源定义的优越性.比如通过Resour ...

  6. 动态加载资源文件(ResourceDictionary)

    原文:动态加载资源文件(ResourceDictionary) 在xaml中控件通过绑定静态资源StaticResource来获取样式Style有多种方式: 1.在项目的启动文件App中<App ...

  7. WPF 杂谈——资源文件

    编写一个应用难免要用到WPF本身的控件.不管是WinForm还是网页都会有自己的控件.只是在写法和用法上有所不同而以.而控件命名却离不开那几个单词.所以不用担心判断不出来哪个是按扭,哪个是文本框.举个 ...

  8. WPF基础篇之静态资源和动态资源

    静态资源(StaticResource)指的是在程序载入内存时对资源的一次性使用,之后就不再访问这个资源了. 动态资源(DynamicResource)指的是在程序运行过程中然会去访问资源. 一.定义 ...

  9. wpf中静态资源和动态资源的区别

    静态资源(StaticResource)指的是在程序载入内存时对资源的一次性使用,之后就不再访问这个资源了. 动态资源(DynamicResource)指的是在程序运行过程中然会去访问资源.

随机推荐

  1. hibernate save,update,saveorupdate方法有什么区别

    save肯定是添加的时候用,update修改时候用,saveorupdate是添加或修改,如果真是这样save和update的存在就没意义了,我们直接saveorupdate就行了. save在添加用 ...

  2. java 23 - 3 单例模式实现Runtime类

    Runtime:每个 Java 应用程序都有一个 Runtime 类实例,使应用程序能够与其运行的环境相连接. 其中一个方法: exec(String command) 在单独的进程中执行指定的字符串 ...

  3. Android应用更换package name以及ui refactoring error问题的有效解决

    package name是Android系统中为每一个应用程序分配的一个标识,每个应用的标识都必须是不同的.在应用开发过程中,有时候可能需要对package name进行修改,这里主要总结修改pack ...

  4. 各种AJAX方法的使用比较

    转:http://www.cnblogs.com/fish-li/archive/2013/01/13/2858599.html#_label6 AJAX技术经过这么多年的发展,出现了一些框架或类库用 ...

  5. Web APP开发技巧总结

    http://www.admin10000.com/document/6304.html 1. 当网站添加到主屏幕后再点击进行启动时,可隐藏地址栏(从浏览器跳转或输入链接进入并没有此效果) <m ...

  6. linux查看出口ip 及w3m字符浏览器

    Linux 查看服务器出口IP    字符浏览器: http://wiki.ubuntu.org.cn/W3m

  7. opencv6.1-imgproc图像处理模块之平滑与形态学操作

    这个部分是<opencv-tutorials.pdf>的部分,这部分也是几大部分中例子最多的,其实这个教程的例子都很不错,不过有些看得出来还是c接口的例子,说明例子有些年头了,其实在&qu ...

  8. 使用markdown编辑evernote(印象笔记)的常用方法汇总

    原文发表在我的博客主页,转载请注明出处 前言 正所谓工欲善其事,必先利其器,本文将要介绍的evernote和markdown都是程序员必备的工具 虽然国内现在有了很多evernote的替代品,做的比较 ...

  9. 前端Mvvm QC 设计解析

    QC 官网http://time-go.github.io/qc/ QC的具体用法.介绍和源码,大家可以去官网下载 从本节开始,我会和大家一起分享在这个框架设计中用到的技巧,希望这些技巧能个大家带来灵 ...

  10. 吉特仓库管理系统-.SQL Server 2012 升级企业版

    随着业务数据的不断增大,单表的数量已经上亿,查询的数据越来越慢,所以考虑到将数据库表分区,同时也将数据库升级到SQL Server 2012. 当时没有考虑更多,在服务器上安装了 SQL Server ...