前提:
-----------------
1.<Label Grid.Column="0" Content="{ComponentResourceKey TypeInTargetAssembly={x:Type res:ItemRes}, ResourceId=Text_ItemID}"/>
2.<Label Grid.Column="0" Content="{x:Static {ComponentResourceKey TypeInTargetAssembly={x:Type res:ItemRes}, ResourceId=Text_ItemID}}"/>
2.5.<Label Grid.Column="0" Content="{StaticResource {ComponentResourceKey TypeInTargetAssembly={x:Type res:ItemRes}, ResourceId=Text_ItemID}}"/>
3.<Label Grid.Column="0" Content="{StaticResource {x:Static {ComponentResourceKey TypeInTargetAssembly={x:Type res:ItemRes}, ResourceId=Text_ItemID}}}"/>
4.<Label Grid.Column="0" Content="{StaticResource {x:Static res:ItemRes.Text_ItemIDKey}}"/>
5.<Label Grid.Column="0" Content="{StaticResource res:ItemRes.Text_ItemIDKey}"/>
6.<Label Grid.Column="0" Content="{DynamicResource res:ItemRes.Text_ItemIDKey}"/>
7.<Label Grid.Column="0" Content="{DynamicResource {x:Static res:ItemRes.Text_ItemIDKey}}"/>
=================================================================================================
用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程序了。