Silverlight (WPF): a preliminary understanding of resources

Source: Internet
Author: User
In WPFProgramData is classified into four levels of storage: data in the database, data in the resource file, data in the WPF object resource, and data in the variable. First, you must understand the definition and use of WPF object-level resources.

1. Define and search for WPF object-level resources
Each WPF interface element has a property named resources. This property is inherited from the frameworkelement class and its type is resourcedictionary. Resourcedictionary can store resources in the form of a "key-value" pair. When you save a resource, resourcedicitionary regards the resource as the object type. Therefore, you must convert the type of the resource object before using the resource.
Resourcedictionary can store any type of objects. InCodeWhen adding resources to resources, the boss adds the correct namespace to The XAML code, as shown in the following code:

<Window X: class = "test. window1 "xmlns =" http://schemas.microsoft.com/winfx/2006/xaml/presentation "xmlns: x =" http://schemas.microsoft.com/winfx/2006/xaml "xmlns: SYS =" CLR-namespace: system; assembly = mscorlib "Title =" test "Height =" 100 "width =" 300 "> <window. resources> <resourcedictionary> <SYS: String X: Key = "str"> test resource usage </sys: String> </resourcedictionary> </window. resources> <stackpanel> <textblock text = "{staticresource resourcekey = STR}" margin = "5"/> </stackpanel> </WINDOW>

Execution result:

First, introduce the system namespace Into The XAML code and map it to the Sys namespace. Then add resource entries in the window. Resources attribute. In the XAML code, the content of the set type and the label extension can be abbreviated, so the code above can modify one of the actions:

<Textblock text = "{staticresource STR}" margin = "5"/>

When retrieving resources, first look for the control's own resources properties. If this resource program does not have it, it will search for the control at the upper level along the logic tree. If the container at the most item layer does not have this resource, the program will find the application. resources. If it cannot be found, an exception is thrown. If you want to use resources defined in The XAML code in the C # code, the format is:

 
String text = (string) This. findresource ("str"); this. textblock1.text = text;

If you know exactly where the resources are placed in the resource dictionary, you can retrieve the resources as follows:

 
String text = (string) This. Resources ["str"]; this. textblock1.text = text;

Resourcedictionary has a source attribute. You only need to assign a file path containing the resource definition to this attribute, and then you can reference or reuse it in a complete set. For example, http://wpf.codeplex.com contains many official/semi-official WPF resources, including the WPF toolkit and a group of pretty skins that are placed in the Xmal file as resources, you only need to add the appropriate XAML file to the project and reference it using the source attribute. For example:

 
<Window. Resources> <resourcedictionary source = "shinyred. XAML"/> </window. Resources>
Reference: http://silverlightchina.net/html/tips/2010/0102/528.html

2. resource usage:
when resources are stored in the resource Dictionary (windows. resource), we can use these resources in the following two ways:
in static and dynamic modes, staticresource is a one-time resource usage when the program loads the memory, and will not be accessed later; dynamicresource means that resources are still accessed while the program is running. Therefore, if we can determine that some resources are used only once during program initialization, staticresource should be used, and resources that may change during program running should be used in the form of dynamicresource. For example, if the color of the program skin remains unchanged during running, you can use it in staticresource mode, if you are allowed to change resources when running the program, you should use them in dynamicresource mode (Dynamic skin replacement like thunder 7 ?).

<Window. resources> <textblockx: Key = "RES1" text = "differences between static and dynamic resource calling"/> <textblockx: key = "RES2" text = "differences between static and dynamic resource calling"/> </window. resources> <stackpanel> <button margin = "4" content = "{staticresource RES1}"/> <button margin = "4" content = "{dynamicresource RES2}"/> <button margin = "4" content = "Update Dynamic Resources" Click = "button_click"/> </stackpanel> void button_click (Object sender, routedeventargs e) {// here We dynamically modify the resource content, so this will be changed unless it is called using dynamicresource. resources ["RES1"] = new textblock () {text = "resource updated"}; this. resources ["RES2"] = new textblock () {text = "resource updated "};}

->

3. Add and Access Binary Resources
In order not to confuse the resources in the resource dictionary with the resources embedded in the application, we clearly call the resources in the resource Dictionary "WPF resources" or "Object resources ", the embedded resources of an application are called "Assembly resources" or "binary resources ". In addition, the resources written in the <application. Resources/> label in the WPF program are still WPF resources rather than binary resources.
If the resource to be added is a string, we can use the resources. resx resource file in the properties namespace of the application. Resources. resx file content is also organized in the form of a "key-value" pair. After compilation, resources. resx will form the resources class in the properties namespace and use the class method or attribute to obtain resources.
To use resources in resources. resx, map the properties namespace of the program to the XAML namespace, and then use the X: static tag extension to access resources:

 
Xmlns: prop = "CLR-namespace: resourcesample. properties" <textblock text = "{X: static prop: resources. Keyword}"/>

The biggest advantage of using resources. resx is that it facilitates program internationalization and localization.
If the added resource is not a string but an external file such as an icon or image, simply right-click to create a folder and add it to the project.

TIPS: To compile an external file into a binary resource, you must set the buildaction attribute value of the file to resource in the attribute window, in addition, the copy to output directory attribute is set to do not copy, and vice versa.

WPF uses the pack URI path to Access Binary resources. The format is as follows:

 
Pack: // application, [/current program name;] [Optional version number;] [Folder name/] File Name

In fact, Pack:/application, can be omitted. The default value is often used for the Assembly name and version number, so we can simply write it.Note the difference between relative paths and absolute paths.

4. supplement resources
Collecting updates.

Reference resources: Reading Notes, msdn

 

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.