What are Staticresources and Dynamicresources in WPF?

Asked

Viewed 54 times

2

When I use Resources in WPF, I can specify them as much as StaticResource, as DyanamicResource. However, most of the time only one works, and the other would initiate an exception.

Starting from this point, I have the following doubts:

  • What is a StaticResource and what is a DynamicResource?
  • When should I use each?
  • Why UWP does not allow use DynamicResources?

1 answer

1


Staticresource is loaded right on the XAML load and is no longer modified. It is preferred because it gives more performance. The whole WPF mechanism knows that you don’t have to keep monitoring it.

Dynamicresource is an expression (is like a variable) that indicates what the resource is, so it has the flexibility to change at runtime which resource is used. Its charge effectively occurs only when the resource needs to be used, so it can even be a resource to be created. Simply change what will result in the expression that the resource can be another. A change will trigger changes throughout the screen’s object tree.

In the documentation from Microsoft has a list of scenarios in which it can be used.

Feature Static Dynamic
Knowledge about resources Development time Running time
Handling of the Resourcedicitionary Not Yes
Reference not known yet Not Yes
Custom themes and controls Internal resources External references
Shared resources Yes Not
Large amount of dependencies Yes Not
Very large resources Not Yes
Values coming from outside Not Yes
Placement in the object tree Fixed position Anywhere

I don’t have conclusive information, but it looks like there are no dynamic resources at UWP because they’ve learned that it brings more trouble than benefits. This can be useful. And I used that some change is possible, and it seems like they’ve been improving some things, but don’t take it for granted.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.