Windows Store App Globalization: Runtime Response language changes

Source: Internet
Author: User

While the application is running, system settings such as language, pixels, contrast, etc. of the system may change, and the application should respond appropriately to changes in the system environment. To solve this problem, you can register the event-handling method for the System state Change event in your application, which triggers the event when changes are made to the system settings information such as language, pixels, and is adjusted accordingly in the event handling method according to the changes in the environment. The following is a concrete example of how to register a state change event in an application and respond to changes to the language preference in the event handling method that is added.

Create a new windows App Store blank app project and named runtimechanges 18.2.3 subsection example.

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 name= "showmessage" content= " Display Information "margin=" 104,365,0,353 "fontsize=" "click=" Showmessage_click "height=" [width= "126"/>

<textblock horizontalalignment= "left" margin= "287,375,0,0" name= "GetResource" fontsize= "" "TextWrapping=" Wrap " Verticalalignment= "Top" height= "width=" 161 "/>

<textblock horizontalalignment=" left "margin=" 104,334,0,0 " textwrapping= "Wrap" text= " Click the button to display the information " fontsize= "" verticalalignment= "Top" height= "26 "Width=" 362 "/>

Next in MainPage.xaml.cs A Click event handling method is implemented in the file for the "Display Information" button Showmessage_click , the code looks like this:

Using Windows.ApplicationModel.Resources.Core;

private void Showmessage_click (object sender, RoutedEventArgs e)

{

    //will beStringresourcethe value of the resource is assigned toGetResourceof a text blockTextProperties

getresource.text = ResourceManager.Current.MainResourceMap.GetValue ("Resources/stringresource"). valueasstring;

< Span lang= "X-none" style= "MSO-FAREAST-LANGUAGE:ZH-CN" >// Register processing method for state Change event

ResourceManager.Current.DefaultContext.QualifierValues.MapChanged + = qualifiervalues_mapchanged;   

}

in theShowmessage_clickmethod, use theResourceManagerClass of Currentin the memberMainresourcesmapmembers are provided byGetValuemethod, the resource pathResources/stringresourceas a parameter to this method, the returned resource value is passedvalueasstringproperty into a string assignment to theGetResourceof a text blockTextproperty so that the resource value is displayed in theGetResourcethe text block.

ResourceManager Class in terms of enumeration and inspection of resources Span lang= "en-US" >resourceloader resourcemanager resourcemanager Windows.ApplicationModel.Resources.Core namespace.

then in the code, for ResourceManager.Current.DefaultContext.QualifierValues.MapChanged Status Change Event Registration event handling method qualifiervalues_mapchanged , which triggers this event when a language preference changes and responds to change information.

here 's a look. qualifiervalues_mapchanged the code implementation of the event-handling method.

Async private void Qualifiervalues_mapchanged (iobservablemap<string, string> sender, imapchangedeventargs< String> @event)

{

    //UseDispatcherof theRunAsyncMethoddisplay the changed language configuration resource values in the interface

await Dispatcher.runasync (Windows.UI.Core.CoreDispatcherPriority.Normal, () =

    {

getresource.text = ResourceManager.Current.MainResourceMap.GetValue ("Resources/stringresource"). valueasstring;

    });

}

In the above method, in order to avoid the use of ui Thread causing confusion, using scheduler dispatcher < Span style= "mso-bookmark:ole_link57" > runasync method to display the changed language configuration resource values into the interface.

run the program to see the effect, when the language preference is Chinese (simplified), click the "Show Information" button, the "Hello World" of Chinese information will be displayed, the effect 18-20 is shown. If you change your language preference to English, the displayed information will immediately become "HelloWorld" in English, as shown in the 18-21 effect .

Figure 18-20 effect when language preference is Chinese (Simplified)

Figure 18-21 language Preference is English-time effect

Using the methods mentioned in this example, you can respond to changes to your language preferences while the app is running, in the first few sections, by restarting the application and responding to changes in the language preferences, which is a significant difference from the previous example. The next section describes how to set language options in your application.

Windows Store App Globalization: Runtime Response language changes

Related Article

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.