Session 16 of WPF (Resources in WPF [2])

Source: Internet
Author: User

In the 13th of this series, we briefly introduced the resources of WPF. However, no specific instance is provided. In this Post, a dynamic resource example is provided, which is also a response to the request of daxian110. And appropriately expand the knowledge not involved in the previous Post.

Let's first look at an example program:

<Window x: Class = "WindowsApplication1.Window1"
Xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml"
Title = "WindowsApplication1" Height = "150" Width = "100" Loaded = "OnLoaded"
>
<Canvas>
<Button Click = "OnClick" Canvas. Left = "10" Canvas. Top = "20"
Width = "80" Height = "30" Content = "{DynamicResource TestRes1}"/>
<Button Canvas. Left = "10" Canvas. Top = "60" Width = "80"
Height = "30" Content = "{DynamicResource TestRes2}"/>
</Canvas>
</Window>

The program is very simple. We add two buttons in the window. We need to pay attention to the Content attribute. This attribute is used to set the content of the button. Why is the name here not Text, but Content? The reason for such naming is related to a very important concept of controls in WPF: almost any controls (that is, elements) in WPF can exist as a container. That is to say, we can include any other Content you want to display in the Content attribute. Not only string text. This abstract processing allows us to treat all the content equally, reducing a lot of processing troubles. In this example, the Content attribute is associated with a TestRes1 and TestRes2. What is TestRes? This is the name of the dynamic resource. The specific content is determined when the button is displayed.

Note that the Loaded attribute in the Window above can be used to set a function name. It is called after loading the Window. Let's take a look at how to use code to control TestRes:

Private void OnLoaded (object sender, RoutedEventArgs e)
{
String szText1 = "Res Text1 ";
This. Resources. Add ("TestRes1", szText1 );

String szText2 = "Res Text2 ";
This. Resources. Add ("TestRes2", szText2 );
}

OnLoaded is a member function in the Window1 class. In this function, we need to add resources because TestRes1 and TestRes2 need to be used in our XAML. If the corresponding resources cannot be found during runtime, the program will fail.

Now, we call the Add method to Add resources. The first parameter is the resource name, and the second parameter is the added resource object.

Program running effect 1:


Figure 1 Figure 2

Next, let's look at how to modify the resource. In the Click attribute of the first button in the above XAML, we specify an OnClick event method. It will be called when you click the button. Now we use this event to modify the Content resource of another button:

Private void OnClick (object sender, RoutedEventArgs e)
{
String szText = "New Res Text ";
This. Resources. Remove ("TestRes2 ");
This. Resources. Add ("TestRes2", szText );
}

OnLoaded implementation is equally simple. First, call the Remove Method to delete existing TestRes2 resources, and then add a new TestRes2 resource object. After you click the first button, the text of the following button is automatically changed to a new resource object. Running Effect 2.

When the XAML loader analyzes the XAML file and finds StaticResource, it searches for the specified Key in the resource of the current Element. If the search fails, it searches up the logic tree until the Root Element. If no resource is found, search for the resource defined by the Application. The resources defined in Application apply to the entire Application. It is similar to a global object. Note: When a Static resource is used, it cannot be referenced forward. Even if the program runs successfully occasionally, the forward reference efficiency will be very low because it needs to find all ResourceDictionay. In this case, DynamicResource is more suitable.

On the other hand, when the XAML loader discovers DynamicResource, it will create an expression based on the current property settings until the resource needs during the running process, and then searches for related content from the resource based on the expression for calculation, returns the required object. Note that the search for DynamicResource is similar to that for StaticResource. In addition to defining the Style and Template, one more search target is displayed. The specific details can be parameter 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.