Windows Store App Globalization: Referencing resources in a detached resource file

Source: Internet
Author: User

most applications require only a single default resource file, such as STRINGS/ZH-CN/RESOURCES.RESW , but in some applications it is best to separate resources into multiple resource files to better organize resource content so that you need to consider how to reference the resources in these detached resource files. The following is an example of how to reference a string resource in a detached resource file in a background code.

Create a newWindowsApp Store's blank app project, and named separatedresourcesfiles. Add the item as 18.2.3the same language folder, resource file, and resource information in the subsection example. When you are done, in theZH-CN"and"en -US"folder and add a name to the"Errors"The resource file, in these twoErrorsadd diagrams to the resource file separately18-11and Figure18-12The string resource shown.


18-11 zh-cn Span style= "font-family:; line-height:14pt; mso-ascii-font-family: "times New roman"; Mso-no-proof:yes "> folder errors Span style= "font-family:; line-height:14pt; mso-ascii-font-family: "times New roman"; Mso-no-proof:yes "> Resources added in resource file

18-12 en-US Span style= "font-family:; line-height:14pt; mso-ascii-font-family: "times New roman"; Mso-no-proof:yes "> folder errors Span style= "font-family:; line-height:14pt; mso-ascii-font-family: "times New roman"; Mso-no-proof:yes "> Resources added in resource file

next in mainpage.xaml file grid textblock to the "times New roman" "> element" Span style= "font-family:; mso-ascii-font-family: "times New roman" "> text block, button to get the value of a resource and display it in one of the textblock text block, another text block is used to display the prompt information, The code looks like this:

<button content=" display information " fontsize= "HorizontalAlignment = "Left" margin= "577,185,0,0" verticalalignment= "Top" height= "" "Width=" 151 "click=" Showmessage_click "/>

<textblock horizontalalignment=" left "margin=" 753,194,0,0 "  textwrapping= "Wrap" name= "separatedresources" verticalalignment= "Top" Height= "35" Fontsize= "" width= "318"/>

<textblock horizontalalignment=" left "margin=" 577,135,0,0 " textwrapping= "Wrap" text= " show the string resource in the detached resource file " fontsize= "25" Verticalalignment= "Top" height= "+" width= "384"/>

in mainpage.xaml.cs showmessage_ Click Separated Resources text block, the code is as follows:

Using Windows.ApplicationModel.Resources;

private void Showmessage_click (object sender, RoutedEventArgs e)

{

    // resourceloader resourceloader

    var Resourceloader = new Resourceloader ("Errors");

    // getstring Span style= "font-family:; Mso-ascii-font-family: "courier new"; Mso-hansi-font-family: "courier new"; MSO-FAREAST-LANGUAGE:ZH-CN "> method get invalidoperation value of the resource

S eparatedResources. Text = resourceloader.getstring ("invalidoperation");

}

In the above code, separate the resource file name "Errors"Creates a new parameter for theResourceloaderobject of the classResourceloaderA parameter that represents the resource file to reference asERRORS.RESW, and then throughResourceloaderobject thatGetStringmethod gets the resource name in the resources fileinvalidoperationvalue is assigned to the separatedResourcesof a text blockTextproperty to display the string resource.

when you set the language preference to Chinese (Simplified), run the program, click the "Show Info" button, and use Simplified Chinese to display "This is an error action", the effect 18-13 is shown. When setting the language preference to English, run the program and click the "Show Info" button to display the "This was anerror operate" in English, as shown in the effect 18-14 .

Figure 18-13 called when the language preference is Chinese (Simplified) Errors resources in the resource file

18-14 language preference is English when you call errors resources in resource file

by the figure18-13and the18-14as you can see, the string resource in the detached resource file was successfully referenced by the above method. This allows you to separate different types of resources into multiple resource files when you design your application so that you can reference different resources in different contexts.

Windows Store App Globalization: Referencing resources in a detached resource file

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.