How to use resources and multi-language settings for WPF applications-2

Source: Internet
Author: User

WPF Resources

WPF resources are actually embedded Resources in the resources format. The default resource name is "Application name. G. Resources". However, WPF resources use the pack URI to access resources.

Add image resources

When the solution Resource Manager contains an image resource (such as data \ img.png), the default output is the resource file (generate operation = resource ), compile the program as a resource during compilation;

When the "generate operation" attribute is set to "content" on the img.png property page, and "Copy to output directory" is set to "copy if new", the output is the content file, data \ img.png copies a copy to the Assembly output directory so that the resource file can be modified without compilation.

The URI link of this image resource is "/data/img.png", for example, <image name = "image3" Source = "/data/img.png"/>

Use strings in the resource dictionary

You can add various types of resources to resoucedictionary. Use the following method to save strings to the resource dictionary. By default, the resource dictionary is compiled into baml and saved to the "Application name. G. Resources" resource. You can also change the resource dictionary to a content file by using the same method.

Resource dictionary XAML code:

<Resourcedictionary 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">
<SYS: String X: Key = "rdstring1"> resource dictionary string. </sys: String>
</Resourcedictionary>

Do not forget to add a reference to this resource dictionary in APP. XAML:

Mergeddictionaries
<Application
Xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml"
X: class = "mysampleapp1.app">
<Application. Resources>
<Resourcedictionary>
<Resourcedictionary. mergeddictionaries>
<Resourcedictionary source = "dictionary1.xaml"/>
</Resourcedictionary. mergeddictionaries>
</Resourcedictionary>
</Application. Resources>
</Application>

 

Use this string in XAML code:

<Label content = "{staticresource rdstring1}" name = "label8"/>

Use this string in the Code:

String MSG = (string) application. Current. findresource ("rdstring1 ");
MessageBox. Show (MSG );

 

Load the WPF Resources in the Assembly

First, prepare a WPF Resource class library: Create a new assembly, delete all the items created by default, add the resource dictionary dictionary1.xaml to the class library, and compile it as classlibrary1.dll, use the reflector tool to check whether the resource name in this class library is classlibrary1.g. resources. The content is dictionary1.baml. OK is ready.

The resource library does not need to be referenced in the main assembly. You only need to put the resource in the same output directory, load the resource in the code, and merge it into the application.

Load Code (relative URI ):

VaR uri = new uri ("/classlibrary1; component/dictionary1.xaml", urikind. Relative );
VaR res = (resourcedictionary) application. loadcomponent (URI );
Application. Current. Resources. mergeddictionaries. Add (RES );

 

Load Code (absolute URI ):

VaR uri = new uri ("pack: // application:,/classlibrary1; component/dictionary1.xaml ");
Resourcedictionary res = new resourcedictionary {source = URI };
Application. Current. Resources. mergeddictionaries. Add (RES );

Load directly in XAML:

<Application
Xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml"
X: class = "mysampleapp. app">
<Application. Resources>
<Resourcedictionary>
<Resourcedictionary. mergeddictionaries>
<Resourcedictionary source = "pack: // application:,/classlibrary1; component/dictionary1.xaml"/>
</Resourcedictionary. mergeddictionaries>
</Resourcedictionary>
</Application. Resources>
</Application>

Use the same method as above. Note that the loaded resource will replace the resource with the same name in the existing resource.

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.