About {DynamicResource {x:Static SystemColors.ControlBrushKey}}
from : http://blog.sina.com.cn/s/blog_749e42850100sahi.html
前提:
<system:String x:Key="{ComponentResourceKey TypeInTargetAssembly={x:Type res:ItemRes}, ResourceId=Text_ItemID}">Item ID</system:String>
public static ComponentResourceKey Text_ItemIDKey { get { return new ComponentResourceKey(typeof(ItemRes), "Text_ItemID"); } }
-----------------
1.<Label Grid.Column="0" Content="{ComponentResourceKey TypeInTargetAssembly={x:Type res:ItemRes}, ResourceId=Text_ItemID}"/>
结果:错误,显示 TargetType=CccData.Resources.ItemRes.ID=Text_ItemID
2.<Label Grid.Column="0" Content="{x:Static {ComponentResourceKey TypeInTargetAssembly={x:Type res:ItemRes}, ResourceId=Text_ItemID}}"/>
结果:Build有错误,下面相对应的关于SystemColors.ControlBrush第2个例子是可以的,x:Static的功能好是拿CLASS里(不是XAML里)的Property, Constants,Variable里的值,但本例中它是XAML里的一个资源
2.5.<Label Grid.Column="0" Content="{StaticResource {ComponentResourceKey TypeInTargetAssembly={x:Type res:ItemRes}, ResourceId=Text_ItemID}}"/> 结果:OK
3.<Label Grid.Column="0" Content="{StaticResource {x:Static {ComponentResourceKey TypeInTargetAssembly={x:Type res:ItemRes}, ResourceId=Text_ItemID}}}"/> 结果:Build有错误 4.<Label Grid.Column="0" Content="{StaticResource {x:Static res:ItemRes.Text_ItemIDKey}}"/>
结果:OK
5.<Label Grid.Column="0" Content="{StaticResource res:ItemRes.Text_ItemIDKey}"/> 结果:Build没有错误,但运行时有错误
6.<Label Grid.Column="0" Content="{DynamicResource res:ItemRes.Text_ItemIDKey}"/> 结果:Build没有错误,但运行时也没有错误,但拿到的是空值
7.<Label Grid.Column="0" Content="{DynamicResource {x:Static res:ItemRes.Text_ItemIDKey}}"/>
结果:OK
其实{shared:SkinObject xxxx}和{StaticResource xxxx}应该是一样的效果。(但没测过)
=================================================================================================
用XAMLPad试验下面一组XAML的编写,你会对x:Static,StaticResource,以及XAML扩展(Markup Extensions)的嵌套用法有一个比较快的认识
0.<Rectangle Name="myRectangle" Width="120" Height="20" Fill="Blue" /> 对应的代码类似-myRectangle.Fill = Brushes.Blue; --OK
1.<Rectangle Name="myRectangle" Width="120" Height="20" Fill="SystemColors.ControlBrush" /> 对应的代码类似-myRectangle.Fill ="SystemColors.ControlBrush" ; --错误, 变成一个字符串,这显然不是你想要的
2.<Rectangle Name="myRectangle" Width="120" Height="20" Fill="{x:Static SystemColors.ControlBrush}" /> 对应的代码类似-myRectangle.Fill =SystemColors.ControlBrush ; --OK
3.<Rectangle Name="myRectangle" Width="120" Height="20" Fill="{StaticResource {x:Static SystemColors.ControlBrush}}" /> 对应的代码类似-myRectangle.Fill =(Brush) myRectangle.FindResource("{#XXXXXX}" ) --错误 ,#XXXXXX 表示你系统当前ControlBrush的颜色
4.<Rectangle Name="myRectangle" Width="120" Height="20" Fill="{StaticResource {x:Static SystemColors.ControlBrushKey}}" /> 对应的代码类似 - myRectangle.Fill =(Brush) myRectangle.FindResource(SystemColors.ControlBrushKey) -OK
5.<Rectangle Name="myRectangle" Width="120" Height="20" Fill="{StaticResource SystemColors.ControlBrushKey}" /> 对应的代码类似 - myRectangle.Fill =(Brush) myRectangle.FindResource("SystemColors.ControlBrushKey") -错误
6.<Rectangle Name="myRectangle" Width="120" Height="20" Fill="{DynamicResource SystemColors.ControlBrushKey}" /> 对应的代码类似 - myRectangle.SetResourceReference(Rectangle.Fill, "SystemColors.ControlBrushKey" ) --错误,但不会报错
7.<Rectangle Name="myRectangle" Width="120" Height="20" Fill="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" /> 对应的代码类似 - myRectangle.SetResourceReference(Rectangle.Fill, SystemColors.ControlBrushKey ) --OK
x:Static--应用于XAML元素的属性语法中,标识其是一个XAML的扩展,其引用的是一个.NET 中静态的值类型。可以说一般是一个枚举的值,或是一个类的静态属性,比如系统颜色类( SystemColors)中的一种颜色。 StaticResource 和 DynamicResource 也都是XAML的一个扩展 两者的区别是DynamicResource 所标识的资源引用会被WPF跟踪,当资源发生变化时,WPF也会自动进行变化(最简单的理解是,屏幕或窗口的颜色,在控制面板中被修改后,如果你应用的是屏幕的颜色,那么WPF也会修改该元素的颜色和属性)。StaticResource 则相对于引用资源一个快照,资源发生变化时,不会自动进行变化。DynamicResource 会比StaticResource 花费多一些的性能,而且不是所有的WPF元素都适合DynamicResource 1. A property/attribute on a FrameworkElement/FrameworkContentElement, which is backed by a DependencyProperty. 2. A value within a Style Setter. 3. A property/attribute on a Freezable, which is provided as a value of either a FrameworkElement/FrameworkContentElement property or a Setter value.
x:Type 也是XAML的一个扩展,最经典的是用在一个Style的TargetType 属性中,这个场景下它相对于一个 typeof() 的操作 例如: <Style TargetType="{x:Type Button}" >
编译时刻 TypeExtension te = new TypeExtension("Button") ; object val = te.ProvideValue( s, Style.TargetTypeProperty) ; //(Object targetObject,Object targetProperty)
运行时刻 Style s = new Style() ; s.TargetType = new typeof( Button) ;
SDK文档上说,x:Type也可以使用在一个属性-元素(property-element )的语法中,但这种情况下TypeName 必须指定,而且一般TypeName是作为x:Type 的一个属性,类似这样的语法<x:Type TypeName="typeName"/>,这个知道一下就行,最多的用法是在 Style的TargetType中,Feb CTP的版本,也有这样的用法<DataTemplate DataType="{x:Type src:AuctionItem}" >
x:Type 的两种语法定义: <Element ... Attribute="{x:Type typeName}" .../> <x:Type TypeName="typeName"/>
XAML中的扩展,都是以{}为标识的,更多的一下XAML扩展可以参考下面的文档,当你搞清楚了这些扩展之后,就能非常容易地看懂别人的XAML程序了。
About {DynamicResource {x:Static SystemColors.ControlBrushKey}}的更多相关文章
- x:Static , StaticResource 和DynamicResource等XAML 扩展用法
原文:x:Static , StaticResource 和DynamicResource等XAML 扩展用法 前提: <system:String x:Key="{Component ...
- WPF 自定义列表筛选 自定义TreeView模板 自定义ListBox模板
有很多项目,都有数据筛选的操作.下面提供一个案例,给大家做参考. 左侧是数据源,搜索框加TreeView控件,右侧是ListBox控件.在左侧数据列点击添加数据,然后点击确定,得到所筛选的数据. 下面 ...
- WPF CheckBox样式 ScrollViewer样式 WrapPanel、StackPanel、Grid布局
本节讲述布局,顺带加点样式给大家看看~单纯学布局,肯定是枯燥的~哈哈 那如上界面,该如何设计呢? 1.一些布局元素经常用到.Grid StackPanel Canvas WrapPanel等.如上这种 ...
- WPF TextBox 搜索框 自定义
更多资源:http://denghejun.github.io <Style x:Key="SearchTextBoxStyle" BasedOn="{x:Null ...
- WPF,Silverlight与XAML读书笔记第四十六 - 外观效果之三皮肤与主题
说明:本系列基本上是<WPF揭秘>的读书笔记.在结构安排与文章内容上参照<WPF揭秘>的编排,对内容进行了总结并加入一些个人理解. 皮肤 皮肤是应用程序中样式与模板的集合,可以 ...
- WPF ItemsControl ListBox ListView比较
在进行列表信息展示时,WPF中提供多种列表可供选择.这篇博客将对WPF ItemsControl, ListBox, ListView进行比较. 相同点: 1. 这三个控件都是列表型控件,可以进行列表 ...
- waterMarkTextBox
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" x ...
- WPF之TreeList的实现方法(一)
做项目的时候根据需求,WPF现有的控件不能完全满足我们的需求, 很多时候我们需要对现有的控件做一下加工. 最简单的我们可能会把Tree转换成List形式有的叫Grid形式就像下图一样 今天我先做一个完 ...
- WPF:xmal 静动态资源
<StackPanel.Resources> <SolidColorBrush x:Key="myBrush" Color="Teal"/&g ...
随机推荐
- win7下安装curl
先去官网下载curl,地址https://winampplugins.co.uk/curl/,我下载的版本是curl_7_52_1_openssl_nghttp2_x64.然后执行curl.exe并且 ...
- 启用div作为编辑器 添加contentEditalbe = “true”,如何让在Html中特殊字符不被转义
今天项目中碰到了两个难以解决的问题,一个是ctrl + enter键换行,enter键发送和支持html特殊字符的发送,二人会话和群会话都必须支持,发送短信的模块也必须支持特殊的字符,但是现在碰到的问 ...
- nrm 的使用说明
nrm -- NPM registry 管理工具 开发的npm registry 管理工具 nrm, 能够查看和切换当前使用的registry, 最近NPM经常 down 掉, 这个还是很有用的哈哈 ...
- 获取Django中model字段名 字段的verbose_name
obj._meta.fields 为关键 obj为model类 推荐使用函数 from django.apps import apps def getmodelfield(appname,modeln ...
- excel保存时出现“请注意,您的文档的部分内容可能包含了文档检查器无法删除的个人信息”
这个问题的原因是由于工作簿包含宏.ActiveX控件等内容, 而Excel被设置为在保存文件时自动删除文件属性中的个人信息,因而出现该对话框.如果要避免出现这个提示,可进行如下设置: Excel 20 ...
- C陷阱与缺陷的学习笔记
1用单引号括起的字符实际代表一个整数,整数值对应于该字符在编译器中采用的字符集对应的序列值:双引号括起的字符串,代表的是一个指向无名数组起始字符的指针,该数组被双引号之间的字符和‘\0’初始化.对于' ...
- 基于jQuery悬停弹出遮罩显示按钮代码
今天给大家分享一款基于jQuery悬停弹出遮罩显示按钮代码.这是是一款当鼠标悬停在图片上后,会出现一系列的按钮,文字等弹性动画效果.效果图如下: 在线预览 源码下载 实现的代码. html代码: ...
- 【Unity笔记】摄像机、图片的模糊处理
重要的参考: http://www.imooc.com/video/7303 摄像机模糊 http://www.xuanyusong.com/archives/3541 http://www.cnbl ...
- beeline执行hql过程中出现错误,权限不足
使用beeline执行hql查询时,出现以下错误: FAILED: Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec ...
- NODE_PATH的疑难杂症(转)
nodejs 中的 NODE_PATH 在使用 nodejs 开发中我们都免不了要去安装一些第三方模块. 那么你或多或少的遇到过以下一些问题 再继续阅读之前,我们先来弄清楚一个概念. npm inst ...