Idle talk about WPF 13th (Resources in WPF)

Source: Internet
Author: User
A resource is an unexecutable data stored in an executable file. Resources allow us to include images, strings, and other data of almost any type. Of course, the. NET Framework also supports such an important feature. A Resource Creation, locating, packaging, and deployment tool is built in it. You can create. resx and. resources files in. NET. Where. resx is composed of XML items .. Resx is only an intermediate format and cannot be directly used by applications. It must be converted to. resource format using tools.

In WPF, the meaning and processing method of resources are different from those of traditional Win32 and Windows Forms resources. First, you do not need to create a. resx file. You only need to specify resources in the project. All other work is done by WPF. Second, the resources in WPF do not have resource IDs like. NET. You need to use URIs to reference resources in XAML. Finally, almost all CLR objects can be contained in WPF resources, as long as the object has a default constructor and independent attributes. In the WPF object, you can declare the following four objects: Style, Brushes, Templates, and DataSource.

Before defining specific resources, consider the following issues:

1. Valid range of resources: in WPF, all framework-level elements (FrameworkElement or FrameworkContentElement) have a Resource attribute. That is to say. We can define attributes in the Resource sub-elements of all such elements. In practice, the three most common elements are the resources corresponding to the three root elements: Application, Page, and Window. As the name suggests, resources defined under the Application root element will be visible to the entire Application and can be accessed. Elements defined in pages and windows can only be accessed in the corresponding pages and windows.

2. Resource Loading Mode: WPF provides two types of resources: Static resources and Dynamic resources.

There are two main differences: A) Static resources are determined at compilation, while Dynamic resources are determined at runtime. B) Static resources determine the corresponding objects or values after the first compilation. It cannot be modified afterwards. Even if the modification is successful, it makes no sense because other resource-using objects will not be notified. Dynamic resources are different. They are searched in the resource target only when they are actually needed during running. Therefore, we can dynamically modify Dynamic resources. Obviously, Dynamic resources are less efficient than Static resources.

3. For both Static and Dynamic resources, you must set the Key attribute: x: Key = "KeyName" for all resources ". Because the resource in WPF does not have a resource ID, it must be identified by the Resource Key to facilitate future resource access. When a range of resources is used, we use StaticResource or DynamicResource tag extension based on the resource type.

Now, after learning about all the resources in WPF, let's look at some simple examples:

<Window

Xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"

Xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml">

<StackPanel>

<StackPanel. Resources>

<SolidColorBrush x: Key = "MyBrush" Color = "gold"/>

</StackPanel. Resources>

<TextBlock Foreground = "{StaticResource MyBrush}" Text = "Text"/>

</StackPanel>

</Window>

In this example, we define a SolidColorBrush Resource in the Resource sub-element of the StackPanel element. Then, you can use StaticResouce to mark the extension and use the previous x: Key attribute to access the defined resources.

Resources can be declared in XAML and accessed through code. Objects that support Resource attributes can be controlled through FindResource, Resource. Add, and Resource. Remove:

<Window

Xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"

Xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml">

<Window. Resouce>

<SolidColorBrush x: Key = "MyBrush" Color = "gold"/>

</Window. Resouce>

</Window>

We first defined a MyBrush in Window. Resource of code XAML. In the code, you can access it as follows:

SolidColorBrush brush = this. FindResource ("MyBrush") as SolidColorBrush;

If you need to modify or delete the resource, you can encode it as follows:

This. Resouce. Remove ("MyBrush"); // deletes the MyBrush resource.

This. Resouce. Add ("MyBrush"); // Add resources dynamically

Note: The reference of this in the above three fields is specifically used to define the element Window of MyBrush. Readers and friends can modify the settings based on the actual situation.

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.