"Reprint WPF Resources"

Source: Internet
Author: User

What do you call WPF of Resources (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)".
Benefits of using resources: 1. Because it is not written in the program code, easy to manage: easy to update. 2. Optimize resources and save space. Once resources are defined, they can be reused. Remember converting an object to a component in Flash? Once the object is converted to a component, it can be reused. In WPF, it seems to have something in common. The difference is that WPF saves resources in XAML, which is "visible" to us.

A simple example of resource usage:

<page name="Root"Xmlns="Http://schemas.microsoft.com/winfx/2006/xaml/presentation"Xmlns:x="Http://schemas.microsoft.com/winfx/2006/xaml"> <Page.Resources> <solidcolorbrush x:key="MyBrush"Color="Gold"/> <style targettype="Border"X:key="PageBackground"> <setter property="Background"Value="Blue"/> </Style> <style targettype="TextBlock"X:key="Label"> <setter property="DockPanel.Dock"Value="Right"/> <setter property="FontSize"Value="8"/> <setter property="Foreground"Value="{StaticResource MyBrush}"/> <setter property="FontFamily"Value="Arial"/> <setter property="FontWeight"Value="Bold"/> <setter property="Margin"Value="0,3,10,0"/> </Style> </Page.Resources> <StackPanel> <border style="{StaticResource PageBackground}"> <DockPanel> <textblock style="{StaticResource Label}">Label</TextBlock> <button dockpanel.dock="Top"Horizontalalignment="Left"Height="30"Background="{StaticResource MyBrush}"Margin="40 "top" Horizontalalignment= "left  "Width=" 100 "Height=" 100 "fill=< Span style= "color: #800000;" > "{staticresource mybrush}" Margin= "40"/> </DockPanel> </Border> </ Stackpanel> </page>            

In the example above, we used static resources (Resource) and, if necessary, we could use 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. It is necessary to note that resources can be accessed and controlled not only in XAML code, but also in C # code. The method is to use FindResource to find resources, resource.add increase resources and resource.remove (remove resources).

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 range: (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.

Resource level

Define Scope

Scope of Use

Object-Level

Can only be applied to this object object or its child objects

FILE-level

In a window or page-level XAML file

All objects that can be applied to this file, such as < Window.Resources

>

Application-level

In the App.xaml document

Apply to anywhere within the application.

Dictionary-level

Encapsulates a resource into a resource dictionary, defined in a resourcedictionary XAML file

It can be reused in another application.

How resources are queried Static Resource query (1) to find the resource dictionary of elements that use the resource, (2) to look up the resource dictionary of the parent element up the logical tree, until the root node, (3) to find the application resource, and (4) to not support forward referencing, That is, you cannot reference a resource that is defined after a reference point.
Dynamic Resource query (1) Find the resource dictionary of 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) finds the application resource, and (4) finds the theme resource dictionary in the current active state;

(5) Find system resources.

"Reprint WPF Resources"

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.