Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
972 views
in Technique[技术] by (71.8m points)

wpf - Is there a significant performance cost to DynamicResource instead of StaticResource?

Our designer is using Blend to style our WPF application. When he chooses local resources for properties, Blend will apply them as a {DynamicResource} instead of a {StaticResource}. My guess is that Blend does this because it enables the app to be re-themed at runtime without having to restart it.

My question is: is there a significant performance cost to this additional lookup? Should we ask the designer to go back and manually change those Dynamics to Statics?

Here is a great SO question that explains the difference between the types: What's the difference between StaticResource and DynamicResource in WPF?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Unfortunately this is a case where it's very hard to do a direct comparison of the relative performance since the place where any degradation would show up is deep in the WPF engine. In the early days of WPF the use of StaticResource was one of the standard performance tuning changes that was recommended and we tended to follow it pretty strictly in our organization and recommend it to others. I was really annoyed that Blend did Dynamic everything even though that helped it to render resources from other files properly at design time.

Over time my view on this has changed, due somewhat to personal experience, but also feedback from people on the Blend team at Microsoft. As you are probably aware, Blend is written completely in WPF and has a complete alternate theme (Light) that can be switched on the fly while the application is running. This is possible because they used DynamicResource for pretty much all of their styling. According to them, this didn't really cause them any real perf issues. Given that Blend is probably the most widely used WPF application in existence, I tend to give significant weight to their views.

The other thing to consider is DynamicResource's actual usefulness. The ability to change styling on the fly is one part of it, but also the flexibility it gives you in building your Resource hierarchy can make it a lot easier to manage shared styles. I'm sure you've run into a situation where a StaticResource reference blew up at runtime because the resource it pointed to was to be loaded in a different branch of the hierarchy.

Obviously StaticResource is very useful for pointing to a specific key that you know is going to be available at the right time. When hand-writing XAML I still tend to use it all the time. But given the productivity you gain out of having a designer generate your XAML in Blend, any small performance gain you might get is probably not worth the overhead of hand-maintaining everything as Static.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...