开发者

Splitting WPF ResourceDictionary

开发者 https://www.devze.com 2023-01-15 02:44 出处:网络
I have a WPF application made up of around seven user controls, each with a variety of different controls on each (textbox, combobox, radio button etc).

I have a WPF application made up of around seven user controls, each with a variety of different controls on each (textbox, combobox, radio button etc).

Across the entire app I have applied a set of control styles take from the WPF Themes on CodePlex, however I have noticed a bit of a slowdown on the app since applying these style开发者_如何学Cs. The ResourceDictionary containing all my styles weighs in at nearly 300kb in code size.

Would there be a benefit to gain by splitting the styles in to multiple Resource Dictionaries and only merging the styles that each usercontrol requires rather than the all the control styles even if they are not being used.

How does WPF load styles in to memory? When required or is the whole ResourceDictionary loaded in to memory at startup?


When an object is created from XAML (or BAML), the XamlReader deserializes all of the XAML's contents. Every XAML element it finds creates an instance of an object - so the Style elements under the UserControl.Resources element generate Style objects that get added to the user control's resource dictionary at the time the object is deserialized. (It's actually a little more complicated than this, since there are apparently parts of XAML deserialization that are asynchronous, but it's close enough for the purposes of this discussion.)

It's important to understand that Resources is not a static property. Every instance of a user control has its own resource dictionary. If you put 300 styles in the user control's XAML, and you create 100 user controls, you'll be creating 30,000 Style objects. This is true whether you're using merged dictionaries or not.

If your resource dictionary is as huge as you say, by far the best thing to do is to put it into the application's Resources property. That way you only pay the price of deserializing each object once.

0

精彩评论

暂无评论...
验证码 换一张
取 消