x:Static , StaticResource 和DynamicResource等XAML 扩展用法
原文:x:Static , StaticResource 和DynamicResource等XAML 扩展用法
前提:
<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程序了。
x:Static , StaticResource 和DynamicResource等XAML 扩展用法的更多相关文章
- StaticResource和DynamicResource
Resource 资源(Resource)是保存在可执行文件中的一种不可执行数据,用来保存一些可以被重复利用的样式,对象定义以及一些传统的资源如二进制数据,图片等等我们可以在任何元素上定义资源 Sta ...
- vue自定义指令clickoutside使用以及扩展用法
vue自定义指令clickoutside使用以及扩展用法 产品使用vue+element作为前端框架.在功能开发过程中,难免遇到使用element的组件没办法满足特殊的业务需要,需要对其进行定制,例如 ...
- XML生成XAMl扩展
所有的WPF控件列为枚举 代码如: 1 public enum ControlType 2 { 3 Window_Resources, 4 Page_Resources, 5 Grid, 6 Stac ...
- WPF中的StaticResource和DynamicResource有什么区别
StaticResource 是静态资源 DynamicResource是动态资源 用一下例子说明 <Window.Resources> <Style x:Key="Bor ...
- 工作总结 for 另类写法 循环加时间 集合合并 也是用的 static class Enumerable (IEnumerable<T>的扩展方法) (IEnumerable<T> 的 工具类) (所有集合 数组都实现IEnumerable<T>)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- EntityFramework.Extended扩展用法
EntityFramework.Extended是一个基于EntityFramework框架 IQueryable类型的扩展方法,包括Update.Delete. 它的优点就是 修改删除操作不仅仅有I ...
- 静态方法块 static 以及对象属性&类属性的用法
使用静态块的好处:只要在类被加载时,static块就会被调用,整个过程就调用这么一次,不会在后面的对象处又不断的调用.如果不使用它,就会出现如下问题:new一个对象,我就要调用一次所需的这些内容,重复 ...
- Android之ExpandableList扩展用法(基于BaseExpandableListAdapter)
1.简介 基于基于BaseExpandableListAdapter扩展的ExpandableList用法,现在网上流行的主要有两种:第一种是向BaseExpandableListAdapter传入两 ...
- SQL join 三种扩展用法
以前学习,只知道 LEFT JOIN.RIGHT JOIN.INNER JOIN.FULL JOIN,共四种集合,然而加上一些条件,可以组合成另外三种集合,直接上图.
随机推荐
- Android 各个版本号WebView
转载请注明出处 http://blog.csdn.net/typename/ powered by miechal zhao : miechalzhao@gmail.com 前言: 依据Googl ...
- 设置secureCRT中vim的字体颜色 分类: B3_LINUX 2014-07-12 22:01 1573人阅读 评论(0) 收藏
1.在/etc/vimrc新增以下一行 syntax on 注:上述方法对root用户无效,原因为在一般用户中,alias vi=vim,而在root用户中默认无此设置,因此若需要root用户也显示颜 ...
- [Angular] Create custom validators for formControl and formGroup
Creating custom validators is easy, just create a class inject AbstractControl. Here is the form we ...
- Swift基础1.1——基本的语法—变量和常量
前些日子.第一届Swift开发人员大会开了之后.身边非常多搞OC的朋友就按捺不住了. 都认为是时候学一下Swift了,毕竟Swift已是趋势. 也是应他们再三要求,让我整理一下Swift的学习心得.今 ...
- android 之修改图片的某一颜色值
首先我们来创建一个叫Image的类,这个类主要用来处理与图有关的一些操作. package org.cn.tools; import java.io.IOException; import java. ...
- 2016.3.14__CSS 定位__第六天
假设您认为这篇文章还不错.能够去H5专题介绍中查看很多其它相关文章. CSS 定位机制 CSS中一共同拥有三种基本定位机制:普通流.浮动.绝对定位. 假设不进行专门指定.全部的标签都在普通流中定位. ...
- poj1066--Treasure Hunt(规范相交)
题目链接:点击打开链接 题目大意:一个正方形的墓葬内有n堵墙,每堵墙的两个顶点都在正方形的边界上,如今这些墙将墓葬切割成了非常多小空间,已知正方形内的一个点上存在宝藏,如今我们要在正方形的外面去得到宝 ...
- [Thu, 9 Jul 2015 ~ Tue, 14 Jul 2015] Deep Learning in arxiv
这一期的神作论文有蛮多的,都很有意思. Feature Representation In ConvolutionalNeural Networks 该论文中论述了在某种CNN结构下,是否有准确率较高 ...
- mysql创建应用账号
-- 赋予某个库全部权限use mysql;grant all privileges on test_db.* to test_user@'%' identified by 'Aa123456';gr ...
- 【u115】&&【t031】 01迷宫
01迷宫(maze01) Time Limit: 1 second Memory Limit: 128 MB [问题描述] 有一个仅由数字0与1组成的n×n格迷宫.若你位于一格0上,那么你可以移动到相 ...