Introduction to resources in WPF, differences between DynamicResource and StaticResource

Source: Internet
Author: User

Source: Introduction to resources in WPF, differences between DynamicResource and StaticResource

What is WPF resource (Resource)?
A resource is a non-executable data that is saved in an EXE file. In WPF resources, almost any CLR object, such as an image, a string, and so on, can be contained , as long as the object has a default constructor and independent properties .

That is, the contents of the application's non-program code, such as bitmap, color, font, animation/movie files, and string constant values, can be separated from the program and packaged as a "resource (Resource)".

Static resources (Resource), dynamic resources. The difference between the two is that a static resource determines its object or value after the first compilation and cannot modify it.

A dynamic resource is determined at run time to find its value in the resource target when it is really needed during the run. Therefore, we can modify it dynamically. Because the runtime of a dynamic resource can determine its value, it is less efficient than a static resource.

Scope of the resource (level):
WPF provides a mechanism for encapsulating and accessing resources (Resource), and we can build resources on different scopes of applications. In WPF, the location of a resource definition determines the available scope of the resource. Resources can be defined in the following ranges:
(1) Object-level: At this point, the resource can only be applied to the object object, or to the object's child objects.
(2) file-level: If the resource is defined in a window or page-level XAML file, it can be applied to all objects in the file.
(3) Application level: If we define the resource in App.xaml, then we can apply the resource to anywhere within the application.
(4) Dictionary level: When we encapsulate a resource into a resource dictionary and define it to a resourcedictionary XAML file, it can be reused in another application.

Each frame-level element (FrameworkElement or FrameworkContentElement) has a resource attribute. Each resource in a resource dictionary has a unique non-duplicate key value (key), which is identified by using the X:key property in the label. In general, the key value is a string, but you can also set it to another object type with the appropriate extension tag. Non-character key value resources are used in specific WPF regions, especially styles, component resources, and style data.

Staticresources for Applications
(1) You no longer need to modify the value of a resource after the resource is first referenced.
(2) Resource references are not recalculated based on the behavior of the runtime, such as when reloading Page/window.
(3) Use StaticResource when the property you want to set is not a DependencyObject or freezable type.
(4) Use StaticResource when you need to compile a resource into a DLL and package it as part of a program, or if you want to share it across applications.
(5) When you need to create a theme for a custom control and use resources in theme, you need to use StaticResource. Because the StaticResource resource looks for behavior when it is predictable, and itself is contained in theme. For DynamicResource, even if the resource is defined in theme, it can only wait for the runtime to be determined, leading to something that might not be expected to happen.
(6) When you need to use a resource to set a large number of dependency properties (Dependency property).
Because dependency properties have a value caching mechanism provided by the property system, if you can set the value of a dependency property when the program loads, the dependency property does not need to check its own value and return the last valid value.

The Dynamic resource is generally used in the following situations
(1) The value of a resource depends on some conditions, and the condition is not determined until run time.
Includes system resources, or resources that can be set by the user. For example, you can create reference system properties such as systemcolors,systemfonts to set values that are dynamic, and whose values come from both the running environment and the operating system.
(2) Reference or create a theme Style for a custom control.
(3) When you want to adjust the contents of a resource dictionary while the program is running.
(4) When you want resources to be referenced forward (as in the canvas, refer to Innerlgbresource)
(5) The resource file is large and you want to load it at run time.
(6) The value of the style to be created may come from other values that depend on the theme or user's settings.
(7) When the parent element of the element that references the resource is likely to change at run time, it also needs to use dynamic resources. Because the parent element changes will result in the scope of the resource query.
Dynamic resource constraints: Properties must be dependency properties, or freezable.

How resources are queried
Static Resource Query
(1) Find the resource dictionary of the elements that use the resource;
(2) Search the logical tree up the resource dictionary of the parent element until the root node;
(3) Find application resources;
(4) Forward references are not supported, that is, resources that are defined after the reference point cannot be referenced.

Query for Dynamic Resource
(1) Find the resource dictionary of the elements that use the resource;
If the element defines a style property, the resource dictionary in the style is found, and if the element defines a template property, the resource dictionary in FrameworkTemplate is found.
(2) The logical tree looks up the resource dictionary of the parent element until the root node;
(3) Find application resources;
(4) Find the theme resource dictionary under the current activation state;
(5) Find system resources.

Example Description:

<window.resources>    <TextBlockx:key= "Res1"Text= "The Moon Is born at sea"></TextBlock>    <TextBlockx:key= "Res2"Text= "The Moon Is born at sea"></TextBlock></window.resources><StackPanel>    <Buttonx:name= "button"Margin= " the"Height= "+"Content="{StaticResource Res1}"/>    <Buttonx:name= "Button1"Margin= " the"Height= "+"Content="{DynamicResource Res2}"/>    <Buttonx:name= "Button2"Margin= " the"Height= "+"Content= "Update"Click= "Button2_Click"/></StackPanel>
Private voidButton2_Click (Objectsender, RoutedEventArgs e) {    //background Operations Resource methods     This. resources["Res1"] =NewTextBlock () {text="Tianya at this time" };  This. resources["Res2"] =NewTextBlock () {Text ="Tianya at this time" };}

Show Results:

Introduction to resources in WPF, differences between DynamicResource and StaticResource

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.