WPF: StaticResource vs DynamicResource

Firstly let us see the official definition of microsoft
  • DynamicResource:
    Provides a value for any XAML property attribute by deferring that value to be a reference to a defined resource. Lookup behavior for that resource is analogous to run-time lookup.
    A DynamicResource will create a temporary expression during the initial compilation and thus defer lookup for resources until the requested resource value is actually required in order to construct an object. This may potentially be after the XAML page is loaded. The resource value will be found based on key search against all active resource dictionaries starting from the current page scope, and is substituted for the placeholder expression from compilation.
  • StaticResource:
    Provides a value for any XAML property attribute by looking up a reference to an already defined resource. Lookup behavior for that resource is analogous to load-time lookup, which will look for resources that were previously loaded from the markup of the current XAML page as well as other application sources, and will generate that resource value as the property value in the run-time objects.
  • Remarks
    A StaticResource must not attempt to make a forward reference to a resource that is defined lexically further within the XAML file. Attempting to do so is not supported, and even if such a reference does not fail, attempting the forward reference will incur a load time performance penalty when the internal hash tables representing a ResourceDictionary are searched. For best results, adjust the composition of your resource dictionaries such that forward references can be avoided. If you cannot avoid a forward reference, use DynamicResource Markup Extension instead.
  • A StaticResource 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.
more specific Instruction from code-project

The difference between StaticResource and DynamicResource lies in how the resources are retrieved by the referencing elements. StaticResource are retrieved only once by the referencing element and used for entire life of the resource. On the other hand, DynamicResource are acquired every time the referenced object is used.

RadialGradientBrush radialGradientBrush = new RadialGradientBrush( Colors.Orange, Colors.Pink);
this.Resources["myGradientBrush"] = radialGradientBrush;
Putting it in simpler way, if the color property of RadialGradientBrush is changed in code to Orange and Pink, then it will reflect on elements only when resource is used as DynamicResource. Below is the code to change the resource in code:

The demerit of DynamicResource is that it reduces application performance because resources are retrieved every time they are used. The best practice is to StaticResource use until there is a specific reason to use

More simlpe instruction from stack-overflow
  • StaticResource will be resolved on object construction.
  • DynamicResource will be evaluated and resolved every time control needs the resource.

posted on 2021-07-29 11:09  不死亡灵首席大元帅  阅读(26)  评论(0编辑  收藏  举报