What's the difference between StaticResource and DynamicResource in WPF?

前端 未结 8 2178
长发绾君心
长发绾君心 2020-11-22 12:33

When using resources such as brushes, templates and styles in WPF, they can be specified either as StaticResources



        
8条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-22 13:30

    What is the main difference. Like memory or performance implications

    The difference between static and dynamic resources comes when the underlying object changes. If your Brush defined in the Resources collection were accessed in code and set to a different object instance, Rectangle will not detect this change.

    Static Resources retrieved once by referencing element and used for the lifetime of the resources. Whereas, DynamicResources retrieve every time they are used.

    The downside of Dynamic resources is that they tend to decrease application performance.

    Are there rules in WPF like "brushes are always static" and "templates are always dynamic" etc.?

    The best practice is to use Static Resources unless there is a specific reason like you want to change resource in the code behind dynamically. Another example of instance in which you would want t to use dynamic resoruces include when you use the SystemBrushes, SystenFonts and System Parameters.

提交回复
热议问题