Windows Store App Globalization: Referencing string resources in background code

Source: Internet
Author: User

The reference string resource is mentioned above in two ways, namely in the XAML element and references a string resource in the resource file in the background code. In the first section, which describes how to Reference string resources in XAML elements, this section explains the relevant knowledge points for referencing string resources in background code.

The following example directly describes how to reference a string resource in a resource file in a background code. Create a new Windows App Store's blank app project and named stringresourcesincode . Create a new project named "Strings "In the folder,"Strings "new folder named"ZH-CN "and"en -USSubfolder and add a resource file to the two subfolders in the zh-cn in the resource file under folder, enter Figure 18-1 The resources shown in en - us in the resource file under folder, enter Figure 18-2 The resources shown.

in mainpage.xaml Grid of files element to add a "Display info" button and two TextBlock text block, button to get the value of a resource and display it in one of the TextBlock in a text block, another block of text is used to display the hint message, as shown in the following code:

<button content=" display information " fontsize= "HorizontalAlignment = "Left" margin= "557,170,0,0" verticalalignment= "Top" height= "" "Width=" "click=" showmessage_ click "/>

<textblock horizontalalignment= "left" margin= "730,185,0,0" textwrapping= "Wrap" name= "showstring" Verticalalignment= "Top" height= "fontsize=" "Width="/>

<textblock horizontalalignment= "left" margin= "555,130,0,0" textwrapping= "Wrap" text= "Click"Display Information"button Display StringResources"fontsize=" verticalalignment= "Top" height= "width=" 327 "/>"

in the MainPage.xaml.cs file, add a click event handling method for the Show Information button Showmessage_click , click the button to display the referenced string resource value, as shown in the following code:

Using Windows.ApplicationModel.Resources;

private void Showmessage_click (object sender, RoutedEventArgs e)

{

    // resourceloader resourceloader

var resourceloader = new Resourceloader ();

    //throughGetStringmethod getsStringresourcethe value of the resource

showstring.text = resourceloader.getstring ("Stringresource");

}

in theShowmessage_clickmethod, create a newResourceloaderobject of the classResourceloader,Resourceloaderclass provides basic access to resource files, referenced libraries, or string resources in other packages,UseResourceloaderclass needs to reference a namespaceWindows.ApplicationModel.Resources. And then throughResourceloader GetString of Objects method Gets the resource file in the resource named stringresource The value assigned to showstring Text of the block property to display the string resource value.

when setting the language preference to Chinese (Simplified), run the program, click the "Show Info" button, use Simplified Chinese to display "Hello World", effect 18-9 is shown. When setting the language preference to English, run the program and click the "Show Info" button to display "HelloWorld" in English, as shown in theeffect 18-10 .

Figure 18-9 The language preference is Chinese (Simplified) time effect

Figure 18-10 language preferences for English time effects

so The two ways to refer to string resources in a resource file are all covered, and developers can choose which method to use to reference the string resource according to the application, and the following describes how to reference resources in the detached resource file.

It is important to note that the resource file and resource content used in this example will be used more than once in the examples in the following chapter, " Hello World "The story is not unfamiliar to programmers, even slightly stale, but in this chapter by deliberately arranging to use different languages to say" Hello World , expressing the desire of the author to hope that the application of the reader can be extended to the whole world.

Windows Store App Globalization: Referencing string resources in background code

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.