【XAML】StaticResource 与 DynamicResource 的区别

来源:互联网 发布:深度卷积神经网络算法 编辑:程序博客网 时间:2024/05/21 14:45

StaticResource  是在加载 XAML 的时候赋值给属性的,发生在程序真正运行之前,而且只会被赋值一次,之后,任何对资源的更改将被忽略。简言之:值只被检索一次然后再整个生命周期中使用。

DynamicResource 是在加载的时候给属性赋值一个表达式对象,但不会真正地去查找对应的资源,直到属性被真正的使用的时候才会去查找。这种特性将资源的查找推迟到了运行过程中真正需要的时刻。一个很好的例子就是事先引用一个在之后才会在XAML中定义的资源,另一个例子就是引用一个只有在运行时才会存在的资源。简言之:值在每次使用的时候都会被重新检索。

 
原文如下:
AStaticResource will be resolved and assigned to the property during the loading of the XAML which occurs before the application is actually run. It will only be assigned once and any changes to resource dictionary ignored.

A DynamicResource assigns an Expression object to the property during loading but does not actually lookup the resource until runtime when the Expression object is asked for the value. This defers looking up the resource until it is needed at runtime. A good example would be a forward reference to a resource defined later on in the XAML. Another example is a resource that will not even exist until runtime. It will update the target if the source resource dictionary is changed.

 

原帖: http://stackoverflow.com/questions/200839/whats-the-difference-between-staticresource-and-dynamicresource-in-wpf

原创粉丝点击