[WPF series]-difference between DynamicResource and StaticResource, wpfstaticresource

Source: Internet
Author: User

[WPF series]-difference between DynamicResource and StaticResource, wpfstaticresource

Discussion:
1. When referencing a resource, consider StaticResource or DynamicResource:
(1) Where can I create resources? (Resource range or level)
A. Is the resource in a Page, Canvas, or Window?
B. In the application scope?
C. In the loose Xaml?
D. In a specific Object (such as a specific Button?
Object level:At this time, the resource can only be applied to this Object, or to the child objects of this Object.
File level:If you define a resource in a Window or Page-level XAML file, you can apply it to all objects in this file.
Application level:If we define a resource in App. xaml, we can apply the resource to any place in the application.
Dictionary level:When we encapsulate resources into a resource dictionary and define a ResourceDictionary XAML file, we can reuse it in another application.

(2) Application functions: Do you want to change resources at runtime?
If you need to change the value, use DynamicResource.
(3) search behavior for different resource reference types. (Do you need to support forward reference ?)
StaticResources:
(1) You do not need to modify the resource value after the resource is referenced for the first time.
(2) Resource references are not re-computed based on the running behavior, for example, when Page/Window is reloaded.
(3) StaticResource is used when the property to be set is not of the DependencyObject or Freezable type.
(4) StaticResource is also used when resources need to be compiled into the dll and packaged as a part of the program or shared among applications.
(5) When you need to create a Theme for a custom control and use resources in Theme, you need to use StaticResource. Because StaticResource resources can be predicted when they are used to search for resources and are included in Theme. For DynamicResource, even if the resource is defined in Theme, it can only be determined at runtime, resulting in unexpected situations.
(6) When you need to use resources to set a large number of Dependency properties.
Because the Dependency Property has the value caching mechanism provided by the property system, if you can set the value of the Dependency Property during program loading, the dependency attribute does not need to check its own value and return the final valid value.
Dynamic Resource is generally used in the following scenarios:
(1) The value of a resource depends on some conditions, which cannot be determined until runtime.
Including system resources or user-configurable resources. For example, you can create reference system attributes such as SystemColors and SystemFonts to set values. These attributes are dynamic and their values come from the runtime environment and operating system.
(2) reference or create Theme Style for the custom control.
(3) When you want to adjust the content of the resource dictionary during the running of the program.
(4) When you want the resource to be referenced forward (for example, the resource is referenced in the Canvas above)InnerLgbResourceSame)
(5) The resource file is large and it is expected to be loaded only at runtime.
(6) The value of the Style to be created may come from other values, and these values depend on Theme or user settings.
(7) When the parent element of the referenced resource element may change at runtime, dynamic resources are also required. The change of the parent element will lead to the resource query range.
Dynamic resource restrictions:The property must be a dependency attribute or Freezable.
Resource Query Method
Static Resource query
(1) Search for the Resource Dictionary of the elements using the Resource;
(2) Search for the resource Dictionary of the parent element along the logic tree until the root node;
(3) Search for Application resources;
(4) Forward references are not supported, that is, resources defined after the referenced point cannot be referenced.
Query Dynamic Resource
(1) Search for the Resource Dictionary of the elements using the Resource;
If an element defines a Style attribute, it searches for the resource dictionary in the Style. If the element defines a Template attribute, it searches for the resource dictionary in the FrameworkTemplate.
(2) Search for the resource Dictionary of the parent element along the logic tree until the root node;
(3) Search for Application resources;
(4) Search for the Theme resource dictionary in the active state;
(5) Search for system resources.

 

Reference

Differences between DynamicResource and StaticResource using instances

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.