Development of New Fashion Windows 8 (7): Resource Qualifier and resource ing

Source: Internet
Author: User

Old week's blog column: http://blog.csdn.net/tcjiaan

For more information, see the original author and source.

 

In the previous article, we blew up the resources and localization, and also made an example. In this article, we will further explore the ing between resource delimiters and resource paths. We may not be very concerned about these two things in actual development. However, it is good to understand them.

These two terms seem to be abstract, or we will feel that they cannot be understood from text descriptions, so we still use experiments to see if abstract concepts can be visualized.

 

1. Start Vs and create a modern-style Application Project (as mentioned above ).

2. In Solution Explorer, right-click the project node and choose add> new item from the shortcut menu. In the template list, find the resource file (. resw), the file name can be determined by default resources.

3. Enter some resources in the resource file just created,

 

4. Save and close the resource file. In addition, create two blank pages in the project, pageqt. XAML and pagemaps. XAML. Now, your solution structure should be similar.

 

(1) Open pageqt. XAML. For the interface layout, refer to the following XAML.

    <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">        <TextBlock Name="tb" FontSize="20" TextWrapping="Wrap" Margin="3"/>    </Grid>

(2) switch to pageqt. XAML. CS, And the C # code is as follows.

Using system; using system. collections. generic; using system. io; using system. LINQ; using Windows. foundation; using Windows. foundation. collections; using Windows. UI. XAML; using Windows. UI. XAML. controls; using Windows. UI. XAML. controls. primitives; using Windows. UI. XAML. data; using Windows. UI. XAML. input; using Windows. UI. XAML. media; using Windows. UI. XAML. navigation; // introduce the following namespace using Windows. applicationModel. resour CES; using Windows. ApplicationModel. Resources. Core; namespace MyApp {// <summary> // you can use it to navigate to a blank page in the frame. /// </Summary> Public sealed partial class pageqt: Page {public pageqt () {This. initializecomponent (); this. loaded + = pageqt_loaded;} void pageqt_loaded (Object sender, routedeventargs e) {resourcecontext context = ResourceManager. current. defaultcontext; string resultstr = string. empty; foreach (VAR item in context. qualifiervalues) {resultstr + = string. format ("{0 }=> {1}", item. key, item. value); resultstr + = "\ n";} This. TB. TEXT = resultstr ;}}}

 

(3) Save and open pagemaps. XAML, as shown in the following figure.

<Page    x:Class="MyApp.PageMaps"    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"    xmlns:local="using:MyApp"    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"    mc:Ignorable="d">    <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">        <TextBlock Name="tb" FontSize="20" TextWrapping="Wrap" Margin="3"/>    </Grid></Page>

Switch to the C # Code view and process the code as shown in the following list:

Using system; using system. collections. generic; using system. io; using system. LINQ; using Windows. foundation; using Windows. foundation. collections; using Windows. UI. XAML; using Windows. UI. XAML. controls; using Windows. UI. XAML. controls. primitives; using Windows. UI. XAML. data; using Windows. UI. XAML. input; using Windows. UI. XAML. media; using Windows. UI. XAML. navigation; // reference the following namespace using Windows. applicationModel. resources; using Windows. applicationModel. resources. core; namespace MyApp {public sealed partial class pagemaps: Page {public pagemaps () {This. initializecomponent (); this. loaded + = (sender, argS) => {string STR = ""; foreach (VAR item in ResourceManager. current. allresourcemaps) {STR + = "--------------------" + item. key + "-------------------- \ n"; foreach (var x in item. value) {STR + = string. format ("{0 }=> {1} \ n", X. key, X. value. uri);} STR + = "\ n";} This. TB. TEXT = STR ;};}}}

5. Now return to mainpage. in XAML, the page root grid is divided into two columns: A ListBox on the left and a frame on the right. I believe you can guess what it is, it is to use frame to navigate and display the previous two pages, one displaying the resource qualifier information and the other displaying the path ing.

<Grid background = "{staticresource applicationpagebackgroundthemebrush}"> <grid. columndefinitions> <columndefinition width = "150"/> <columndefinition/> </grid. columndefinitions> <ListBox name = "LB" grid. column = "0" selectionchanged = "onselectionchanged"> <listboxitem> resource qualifier </listboxitem> <listboxitem> resource ing </listboxitem> </ListBox> <frame X: name = "frameright" grid. column = "1" margin = "2"/> </GRID>

[C # code]

Using system; using system. collections. generic; using system. io; using system. LINQ; using Windows. foundation; using Windows. foundation. collections; using Windows. UI. XAML; using Windows. UI. XAML. controls; using Windows. UI. XAML. controls. primitives; using Windows. UI. XAML. data; using Windows. UI. XAML. input; using Windows. UI. XAML. media; using Windows. UI. XAML. navigation; // "blank page" item template in http://go.microsoft.com/fwlink? On linkid = 234238, the introduction of namespace MyApp {// <summary> /// can be used to itself or navigate to a blank page inside the frame. /// </Summary> Public sealed partial class mainpage: Page {public mainpage () {This. initializecomponent () ;}/// <summary> /// call when this page is to be displayed in the frame. /// </Summary> /// <Param name = "E"> describes how to access event data on this page. Parameter // properties are usually used on the configuration page. </Param> protected override void onnavigatedto (navigationeventargs e) {lb. selectedindex = 0;} private void onselectionchanged (Object sender, selectionchangedeventargs e) {ListBox lB = sender as ListBox; If (Lb! = NULL) {int Index = LB. selectedindex; Switch (INDEX) {Case 0: This. frameright. navigate (typeof (pageqt); break; Case 1: This. frameright. navigate (typeof (pagemaps); break; default: This. frameright. navigate (typeof (pageqt); break ;}}}}}

 

6. Open package. appxmanifest in the configuration file, switch to the "package" tab, and change the package name to a nice-looking name. Later.

Run it now.

 

 

In the first article, we can probably know what a resource qualifier is. In our example in the previous article, we used a language identifier in a short/Traditional Chinese switch. We can see that the delimiters are:

Language: The preferred language for the current application;

Contrast: contrast.

Scale: scale ratio.

Homeregion: region.

And so on.

If you want to learn more about the qualifier, you can refer to the official documentation.

Http://msdn.microsoft.com/zh-cn/library/windows/apps/hh965372.aspx

There is no skill in this. You just need to follow the instructions in this document.

 

Let's take a look at path ing. we modified the package name to check the package name here,

 

I don't know what rules have you found above?

1. The resource path starts with "MS-Resource.

2. the resources that the application considers are not just the. resw file, but almost all the files in our project are listed.

3. When referencing a resource, we reference it by key, that is, the content on the left of "=>" in the figure.

4. Focus on the resources ing of resources. resw. We can see that the reference name of resources. resw is resources. This name is the default one. Do you remember the example in the previous article? When we instantiate resourceloader, it calls a non-parameter constructor, because resources is the default value. If the resource file we add is ABC. resw, so when we instantiate resourceloader, we can't use a non-argument constructor. Instead, to pass a reference name, the reference name should be ABC according to the rules obtained in the above experiment.

The two resource items under resources are mapped to different Uris, resources/key1 and resources/key2, you should know how to do it.

The resourcemap class has a getsubtree method that can return the specified referenced sub ing. For example, if getsubtree ("Resources") is called, only key1 and key2 are returned for the returned map, which is the resource item we are looking for. Then, on the returned resourcemap, call getvalue ("key1") to obtain the resource.

 

The following example shows how to retrieve the value of the resource item from the resourcemap ing.

1. Create a project.

2. Complete the layout in mainpage. XAML.

<Grid background = "{staticresource applicationpagebackgroundthemebrush}"> <stackpanel margin = "25"> <textblock fontsize = "20" text = "first value: "/> <textblock fontsize =" 24 "name =" tb1 "/> <textblock fontsize =" 20 "margin =", "text =" second value: "/> <textblock fontsize =" 24 "name =" tb2 "/> </stackpanel> </GRID>

3. Switch to the C # Code view.

        protected override void OnNavigatedTo(NavigationEventArgs e)        {            ResourceMap map = ResourceManager.Current.MainResourceMap.GetSubtree("Resources");            tb1.Text = map.GetValue("v1").ValueAsString;            tb2.Text = map.GetValue("v2").ValueAsString;        }

Then, run the program to view the effect.

 

 

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.