Resources in WPF are Shared and Lazy by default

November 17, 2010

4 comments

I’ve just read a post of Christian Mosers listing “Top 10 WPF Performance Tips”. I agree with most of them, as they are general tips, but except for one:

Load resources when needed. Even thow it’s the most comfortable way to merge all resources on application level it can also cost performance by loading all resources at startup. A better approach is to load only often used resources and load the other on view level”

Resources in WPF are lazy load and shared by default. If you place a resource in the application resources from XAML or from any other XAML resources it won’t be loaded until someone reference it using StaticResource or DynamicResource (there are other ways too).

To demonstrate that, I’ve created a very simple application, defines three types: A simple CLR class, a Freezable one and DependecyObject. I’ve created an instance of each one of these types inside the App.xaml resource dictionary. Each type logs a message from within the constructor. Running the application which by default displays the MainWindow you may notice that no instance has been created.

I’ve added four test cases: The first one instantiates a window which uses the Simple class resource, the second uses the Freezable and the third the DependecyObject. Each test case invokes only the relevant resource and results in only one instance, no matter how many windows where created. what’s the type of it. The fourth test case creates a Window which uses the Simple resource but with the x:Shared=”False” XAML parser attribute. This prevents resource sharing.

From that you can learn that:

  1. Resources in WPF are lazy load. They are loaded only when you reference them.
  2. Resources in WPF are shared by default like singleton objects.
  3. You can prevent a resource from being shared by using the x:Shared=”False” XAML attribute.

You can download the tester from here.

Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*

4 comments

  1. elad katzNovember 17, 2010 ב 23:55

    great post!

    i always thought that resources are created when they are parsed – it’s very good to know it’s not… but how is it implemented? i thought that the xaml parser just creates an instance and put it inside the resources property of the relevant object… and that it’s just regular deserializing.. and i guess it’s not the ‘magic’ doing of DependencyProperties either, since it’s working with StaticResource also?! how very cool.. but how?

    Reply
  2. elad katzNovember 17, 2010 ב 23:56

    and btw, for some reason i can’t post here using Chrome – the captcha is not allowing it..

    Reply
  3. Tomer ShamamNovember 18, 2010 ב 20:25

    Elad, try to use IE or Firefox. Instead of you I wouldn’t expect too much from Google Chrome… 😉

    Reply
  4. elad katzNovember 19, 2010 ב 20:16

    silly me… and i always thought that captchas were supposed to refute robots, not browsers 😉

    Reply