StaticResource

  • ThemeResource

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

  1. <Page
  2. x:Class="Windows10.Resource.StaticResourceDemo"
  3. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  4. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  5. xmlns:local="using:Windows10.Resource"
  6. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  7. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  8. mc:Ignorable="d"
  9.  
  10. xmlns:common="using:Windows10.Common">
  11.  
  12. <!--
  13. 定义各种类型的 StaticResource(Resources 属性来自 FrameworkElement)
  14. 系统会在加载 xaml 的时候去查找并应用 StaticResource 指定的资源
  15. StaticResource 需要先定义后引用,比如如果把下面这段 Page.Resources 代码放到 Grid 后面去定义,那么就会报错
  16. -->
  17. <Page.Resources>
  18. <x:String x:Key="MyString">StaticResource 经常用于指定 Style 或 ControlTemplate 资源,参见 /Controls/UI 部分</x:String>
  19. <x:Double x:Key="MyDouble1">24</x:Double>
  20. <x:Double x:Key="MyDouble2">48</x:Double>
  21. <Thickness x:Key="MyThickness">20,20,20,20</Thickness>
  22. <common:Employee x:Key="CurrentEmployee" Name="wanglei" Age="35" IsMale="True"></common:Employee>
  23.  
  24. <!--静态资源也可以用来定义控件-->
  25. <Flyout x:Key="MyFlyout">
  26. <StackPanel>
  27. <TextBlock Text="我是 Flyout 中的内容" />
  28. </StackPanel>
  29. </Flyout>
  30. </Page.Resources>
  31.  
  32. <Grid Background="Transparent">
  33. <StackPanel Margin="10 0 10 10">
  34.  
  35. <!--
  36. 下面演示 StaticResource 的使用方法
  37. -->
  38.  
  39. <!--
  40. StaticResource 的 3 种引用方式
  41. -->
  42. <TextBlock Name="textBlock0" Margin="5" Text="{StaticResource MyString}" />
  43. <TextBlock Name="textBlock1" Margin="5" Text="{StaticResource ResourceKey=MyString}" />
  44. <TextBlock Name="textBlock2" Margin="5">
  45. <TextBlock.Text>
  46. <StaticResource ResourceKey="MyString" />
  47. </TextBlock.Text>
  48. </TextBlock>
  49.  
  50. <TextBlock Name="textBlock3" Margin="5" FontSize="{StaticResource MyDouble1}" Text="我是 TextBlock">
  51. <!--
  52. Style 或 ControlTemplate 内都可以引用静态资源
  53. -->
  54. <TextBlock.Style>
  55. <Style TargetType="TextBlock">
  56. <Setter Property="Padding" Value="{StaticResource MyThickness}" />
  57. </Style>
  58. </TextBlock.Style>
  59. </TextBlock>
  60.  
  61. <!--
  62. 动态改变引用的资源
  63. -->
  64. <Button Name="btnChangeStaticResource" Content="改变引用的 StaticResource" Margin="5" Click="btnChangeStaticResource_Click" />
  65.  
  66. <!--
  67. 设置 FrameworkElement 的 DataContext 为一个指定的静态资源
  68. -->
  69. <TextBlock Margin="5" DataContext="{StaticResource CurrentEmployee}" Text="{Binding Name}" />
  70. <TextBlock Margin="5" DataContext="{StaticResource CurrentEmployee}" Text="{Binding Age}" />
  71. <TextBlock Margin="5" DataContext="{StaticResource CurrentEmployee}" Text="{Binding IsMale}" />
  72.  
  73. <!--
  74. 引用指定的静态资源(此静态资源是一个控件)
  75. -->
  76. <Button Margin="5" Content="按我弹出 Flyout" Flyout="{StaticResource MyFlyout}" />
  77.  
  78. <!--
  79. 如果一个 FrameworkElement 要引用内部 Resources 的话,像下面这么写是会报错的,因为资源被先引用后定义了
  80. -->
  81. <!--
  82. <TextBlock Margin="5" Text="我是 TextBlock" Foreground="{StaticResource MyTextBlockForeground}">
  83. <TextBlock.Resources>
  84. <SolidColorBrush x:Key="MyTextBlockForeground" Color="Red" />
  85. </TextBlock.Resources>
  86. </TextBlock>
  87. -->
  88.  
  89. <!--
  90. 如果一个 FrameworkElement 要引用内部 Resources 的话,需要像下面这么写(资源先定义后引用)
  91. -->
  92. <TextBlock Margin="5" Text="我是 TextBlock">
  93. <TextBlock.Resources>
  94. <SolidColorBrush x:Key="MyTextBlockForeground" Color="Red" />
  95. </TextBlock.Resources>
  96. <TextBlock.Foreground>
  97. <StaticResource ResourceKey="MyTextBlockForeground" />
  98. </TextBlock.Foreground>
  99. </TextBlock>
  100.  
  101. </StackPanel>
  102. </Grid>
  103. </Page>

Resource/StaticResourceDemo.xaml.cs

  1. /*
  2. * 演示“StaticResource”相关知识点
  3. */
  4.  
  5. using Windows.UI.Xaml;
  6. using Windows.UI.Xaml.Controls;
  7.  
  8. namespace Windows10.Resource
  9. {
  10. public sealed partial class StaticResourceDemo : Page
  11. {
  12. public StaticResourceDemo()
  13. {
  14. this.InitializeComponent();
  15. }
  16.  
  17. private void btnChangeStaticResource_Click(object sender, RoutedEventArgs e)
  18. {
  19. // 获取 Application 中的资源
  20. // (double)Application.Current.Resources["MyDouble1"];
  21.  
  22. // 获取关联 xaml 内的资源(本例中的资源定义在 xaml 中的 Page 下,所以用 this.Resources[""] 来获取)
  23. if (textBlock3.FontSize == (double)this.Resources["MyDouble1"])
  24. {
  25. // 引用指定的资源
  26. textBlock3.FontSize = (double)this.Resources["MyDouble2"];
  27. }
  28. else
  29. {
  30. textBlock3.FontSize = (double)this.Resources["MyDouble1"];
  31. }
  32. }
  33. }
  34. }

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

  1. <Page
  2. x:Class="Windows10.Resource.ThemeResourceDemo"
  3. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  4. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  5. xmlns:local="using:Windows10.Resource"
  6. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  7. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  8. mc:Ignorable="d">
  9.  
  10. <!--
  11. ThemeResource 与 StaticResource 的区别是:ThemeResource 在运行时会根据主题的变化而重新计算
  12. -->
  13.  
  14. <!--
  15. 默认的主题资源的相关定义在如下位置(以我的开发环境为例)
  16. 1、C:\Program Files (x86)\Windows Kits\10\DesignTime\CommonConfiguration\Neutral\UAP\10.0.10586.0\Generic\generic.xaml
  17. 2、C:\Program Files (x86)\Windows Kits\10\DesignTime\CommonConfiguration\Neutral\UAP\10.0.10586.0\Generic\themeresources.xaml
  18.  
  19. 注:默认的主题资源不会复制到应用程序中,这些资源字典在内存中作为 Windows 运行时本身的一部分存在
  20. -->
  21.  
  22. <Page.Resources>
  23.  
  24. <ResourceDictionary>
  25. <!--
  26. 通过 ResourceDictionary 内的 ResourceDictionary.ThemeDictionaries 内的 ResourceDictionary 来定义不同主题的资源
  27. 在资源中定义的主题分为 3 种:"Light", "Dark" 和 "HighContrast",其中 High Contrast(高对比度模式) 不常用,就不详细介绍了
  28. -->
  29. <ResourceDictionary.ThemeDictionaries>
  30.  
  31. <!--
  32. Default 主题,对应 ElementTheme.Dark 或 ApplicationTheme.Dark
  33. -->
  34. <ResourceDictionary x:Key="Default">
  35. <!--
  36. 这里摘自 themeresources.xaml 中的部分定义,如果要覆盖其中的定义就自己再定义同名资源即可
  37. -->
  38. <SolidColorBrush x:Key="ApplicationPageBackgroundThemeBrush" Color="#FF000000" />
  39. <SolidColorBrush x:Key="SystemControlBackgroundAccentBrush" Color="{ThemeResource SystemAccentColor}" />
  40.  
  41. <!--
  42. 这是系统级资源,不在 themeresources.xaml 内,其含义是在“设置”->“个性化”->“颜色”中选择的主题色,当然也可以这样重新定义
  43. -->
  44. <Color x:Key="SystemAccentColor">#FFFF0000</Color>
  45. </ResourceDictionary>
  46.  
  47. <!--
  48. HighContrast 主题,不常用,就不详细介绍了
  49. -->
  50. <ResourceDictionary x:Key="HighContrast">
  51. <!--
  52. 这里摘自 themeresources.xaml 中的部分定义,其引用的一些颜色资源来自系统级,比如 SystemColorWindowColor 或 SystemColorButtonFaceColor 之类的,他们不在 themeresources.xaml 内
  53. 比如在“设置”->“轻松使用”->“高对比度”中目前可以设置 4 中不同的高对比度主题,每一种对应的颜色资源都不一样
  54. -->
  55. <SolidColorBrush x:Key="ApplicationPageBackgroundThemeBrush" Color="{ThemeResource SystemColorWindowColor}" />
  56. <SolidColorBrush x:Key="SystemControlBackgroundAccentBrush" Color="{ThemeResource SystemColorButtonFaceColor}" />
  57. </ResourceDictionary>
  58.  
  59. <!--
  60. Light 主题,对应 ElementTheme.Light 或 ApplicationTheme.Light
  61. -->
  62. <ResourceDictionary x:Key="Light">
  63. <!--
  64. 这里摘自 themeresources.xaml 中的部分定义,如果要覆盖其中的定义就自己再定义同名资源即可
  65. -->
  66. <SolidColorBrush x:Key="ApplicationPageBackgroundThemeBrush" Color="#FFFFFFFF" />
  67. <SolidColorBrush x:Key="SystemControlBackgroundAccentBrush" Color="{ThemeResource SystemAccentColor}" />
  68.  
  69. <!--
  70. 这是系统级资源,不在 themeresources.xaml 内,其含义是在“设置”->“个性化”->“颜色”中选择的主题色,当然也可以这样重新定义
  71. -->
  72. <Color x:Key="SystemAccentColor">#FF00FF00</Color>
  73. </ResourceDictionary>
  74.  
  75. </ResourceDictionary.ThemeDictionaries>
  76. </ResourceDictionary>
  77.  
  78. </Page.Resources>
  79.  
  80. <Grid Background="Transparent">
  81.  
  82. <StackPanel Name="panel" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" Margin="10 0 10 10">
  83.  
  84. <TextBlock Margin="5" Name="lblMsg" Foreground="Blue" />
  85.  
  86. <TextBlock Margin="5" Text="Microsoft HoloLens全息眼镜由Microsoft 公司于北京时间2015年1月22日凌晨与Window10同时发布。" Foreground="Blue" />
  87.  
  88. <StackPanel Width="200" Height="100" Margin="5" HorizontalAlignment="Left" Background="{ThemeResource SystemControlBackgroundAccentBrush}" />
  89.  
  90. <!--动态变换主题,引用的主题资源会重新计算-->
  91. <Button Name="btnChangeTheme" Click="btnChangeTheme_Click" Margin="5">变换主题</Button>
  92.  
  93. </StackPanel>
  94.  
  95. </Grid>
  96. </Page>

Resource/ThemeResourceDemo.xaml.cs

  1. /*
  2. * 演示“ThemeResource”相关知识点
  3. *
  4. *
  5. * 1、主题共有两种类别:Light 和 Dark,子会继承父的主题类别
  6. * 2、Application 级别指定 Theme 的话,在 App.xaml 中做如下声明 <Application RequestedTheme="Dark"></Application>
  7. * 3、FrameworkElement 级别指定 Theme 的话,则指定 FrameworkElement.RequestedTheme 即可
  8. *
  9. *
  10. * Application.Current.RequestedTheme - 获取或设置 Application 级别的主题(ApplicationTheme 枚举:Light, Dark)
  11. * FrameworkElement.RequestedTheme - 获取或设置 FrameworkElement 级别的主题(ElementTheme 枚举:Default, Light, Dark)
  12. * 注:ElementTheme 比 ApplicationTheme 多了一个 Default,其含义是当 ElementTheme 为 Default 时,其实际主题为 application 级主题
  13. */
  14.  
  15. using System;
  16. using Windows.UI.Xaml;
  17. using Windows.UI.Xaml.Controls;
  18.  
  19. namespace Windows10.Resource
  20. {
  21. public sealed partial class ThemeResourceDemo : Page
  22. {
  23. public ThemeResourceDemo()
  24. {
  25. this.InitializeComponent();
  26.  
  27. DisplayMessage();
  28. }
  29.  
  30. private void DisplayMessage()
  31. {
  32. // 当前 Application 级别的 Theme
  33. lblMsg.Text = "application theme: " + Application.Current.RequestedTheme.ToString();
  34. lblMsg.Text += Environment.NewLine;
  35.  
  36. // 当前 panel 的 Theme
  37. lblMsg.Text += "FrameworkElement theme: " + panel.RequestedTheme.ToString();
  38. }
  39.  
  40. // 动态变换主题,引用的主题资源会重新计算
  41. private void btnChangeTheme_Click(object sender, RoutedEventArgs e)
  42. {
  43. if (panel.RequestedTheme == ElementTheme.Default) // 未指定 panel 的主题,则 panel 主题同 application 级主题
  44. {
  45. if (Application.Current.RequestedTheme == ApplicationTheme.Dark) // application 是 Dark 主题
  46. {
  47. panel.RequestedTheme = ElementTheme.Light;
  48. }
  49. else
  50. {
  51. panel.RequestedTheme = ElementTheme.Dark;
  52. }
  53. }
  54. else if (panel.RequestedTheme == ElementTheme.Dark) // panel 是 Dark 主题
  55. {
  56. panel.RequestedTheme = ElementTheme.Light;
  57. }
  58. else // panel 是 Light 主题
  59. {
  60. panel.RequestedTheme = ElementTheme.Dark;
  61. }
  62.  
  63. DisplayMessage();
  64. }
  65. }
  66. }

App.xaml

  1. <Application
  2. x:Class="Windows10.App"
  3. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  4. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  5. xmlns:local="using:Windows10"
  6.  
  7. RequestedTheme="Dark">
  8. <!--
  9. 上面的 RequestedTheme 被我设置为 Dark 了,关于主题的相关知识点请参见:/Resource/ThemeResourceDemo.xaml
  10. -->
  11.  
  12. </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. Eclipse调整双击选取的字符颜色背景

    Eclipse调整双击选取的字符颜色背景,如下图所示: 会有二点影响: 1. 编辑页的颜色 2. 右侧滚动条的小提示点的颜色.

  2. bzoj1036 [ZJOI2008]树的统计Count

    1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec  Memory Limit: 162 MB Submit: 12646  Solved: 5085 [Subm ...

  3. css的小问题总结

    1.居中问题 比如让宽度为60%的<div class="box">居中,可以再.box里面设置margin:auto 2.高度固定的div里面有两个子类div且高度和 ...

  4. Anyconnect的VPN环境部署(1)-OpenConnect server(ocserv)服务安装

    打算在公司IDC机房部署一套VPN环境,经过考虑,最终决定采用Cisco下的开源技术AnyConnect.AnyConnect的优势有:1)长连接,待机不会断开:2)速度快,稳定性好:3)安全性好,全 ...

  5. 查看mysql表结构和表创建语句的方法(转)

    查看mysql表结构的方法有三种:1.desc tablename;例如:要查看jos_modules表结构的命令:desc jos_modules;查看结果:mysql> desc jos_m ...

  6. 10 个迅速提升你 Git 水平的提示

    1. Git自动补全 假使你使用命令行工具运行Git命令,那么每次手动输入各种命令是一件很令人厌烦的事情.为了解决这个问题,你可以启用Git的自动补全功能,完成这项工作仅需要几分钟. 为了得到这个脚本 ...

  7. c#:Reflector+Reflexil 修改编译后的dll/exe文件

    不知道大家有没有这样的经历:现场实施时测试出一个bug,明明知道某个dll/exe文件只要修改一二行代码即可,但手头没有开发环境,紧急情况下,可以用reflector + reflexil 临时直接修 ...

  8. 记、基于react-router的单页应用

    现在用react写单页应用基本上都是用react-router做前端路由了吧!最近在使用react-router的过程中遇到了不少问题,在这里总结一下. 浏览器url react-router默认提供 ...

  9. 为什么带网格(mesh)的模型添加了刚体Rigidbody和MeshCollider,还是会从地板穿过去?

    两个Gameobject 放置在空中, 一个是Cube,一个是茄子模型 Cube的Collider 是Box Collider , 茄汁的Collider 是mesh collider, 他们都添加了 ...

  10. Codeforces Round #358(div 2)

    A:统计个数题,要注意ans+=a*b+c*d中,如果a*b>int,那么即使ans是long long也会越界,所以ans+=(long long)a*b+(long long)c*d B:模 ...