May 03

WPF Difference between Static Resource and Dynamic Resource

WPF

WPF Difference between Static Resource and Dynamic Resource

There are lot of differences between Static Resource and Dynamic Resource in WPF. Lets try to understand them with the help of examples below:

  1. The major difference between static and dynamic resources is that static resource will evaluate the resource only once while dynamic resource will be evaluated every time the resource needed.
  2. Dynamic resource has more performance overhead than static resources because it look up for resources every time it requested or needed.
  3. Static resource is faster but it takes little more time to load page or window than dynamic resource because dynamic resources are loaded when you actually use them.

Example of Static and Dynamic Resources

Below example gives you clear picture about Static and Dynamic resource markup extension.

Output
Static and Dynamic resrouces

As shown in above example, three buttons are using buttonBackground resource defined in windows.resources element. Button1 and Button3 are using static resource markup extension while button2 is using dynamic resource markup extension. After loading of my window, I have changed color of buttonBackground resource from Lightblue to Yellow in code behind. So when I run my application, Button2 will have yellow background and rest of the buttons will have LightBlue background. The reason behind for not changing background of Button1 and Button3 because both button uses static resource and static resource is loaded only once with application while button2 uses dynamic resource and it changes every time when it accessed.

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

Permanent link to this article: http://zappmania.in/2014/05/03/wpf-difference-static-resource-dynamic-resource.htm

1 comment

1 ping

    • SURIYA on August 19, 2018 at 6:53 pm
    • Reply

    Nice One , Very Useful Thank You..Clear Example..

    • hermes ceinture femme on May 16, 2014 at 2:26 am

    hermes ceinture femme

    I’m extremely inspired together with your writing abilities and also with the format on your weblog. Is this a paid theme or did you customize it yourself? Anyway keep up the nice quality writing, it’s rare to peer a great blog like this one

Leave a Reply

Your email address will not be published.