"Windows 10 app development" elaborate text resource file (RESW)

Source: Internet
Author: User
Tags tag name

Recently, operas bone version of the Dream of Red mansions is very hot, the old weeks after reading 9 episodes, has been seen Surface of the fan sound. Lin Daiyu and Xue Bao the two characters are not very good, but the Shi Xiangyun doll play well, the old week is very like.

So, last night did not write code, let the machine also rest. Some people will ask, watching video can see fever? Yes, as a result, the weather in the south, although the autumnal equinox has been past, but still heat, and secondly, the old week is on the Web page, Flash that playback components, you know, the performance of special.

Well, F won't be a lot of talk, today we're talking about UWP development, a more important, can be a lot of people do not go back to the point of knowledge-text resources.

Don't think this is useless, and when you develop apps that require multiple language UIs, it can be pretty damn useful. For example, Chinese version, English version, French version, chicken version, dog language version.

In a UWP app, a text resource has a special type of file to store, with a suffix of. RESW, and the "W" behind it may be the meaning of Windows, or it might be Word. Regardless of it, anyway, you know that this kind of file is dedicated to storing text resources. In fact, the RESW file is essentially an XML document, similar to a. resx file in a. NET project. The schema of the Resw file is exactly the same as the. resx, except that it can only include resource entries of the string type.

This is a purely technical thing, you know the old week, the old week has always special don't like to say too much convoluted theory, to get something, I just know what it is, what to use, I use is enough, in fact, the old weeks do not care.

It is estimated that many people who learn programming will, like the old week, be eager to use it immediately. OK, let's talk about it in the next week.

How to create a text resource

Since the text resource is a RESW file, of course, "New item" in the project, this will use it, do not I say, if you do not use VS, then you can not develop the UWP app, it is necessary to learn some kindergarten courses.

In the new Item window, you will find an item called "Resource file", such as.

Again seriously, this resource file is to put text content dedicated, not to put the film, remember, put the film you can directly in the form of files in the Assets directory, or you build your own directory.

The old week's demonstration project is such, first in the project to build a directory called Textdecs, directory name is random, not beautiful, please forgive, originally wanted to call "Little Wei", but worried about too many strokes, after seeing unclear. Then two. resw files are built below. As shown in.

You must have guessed that the old week would want to be a simplified Chinese version and a traditional Chinese version. Yes, you're smart. Why not get the English version? Is this, the old week in the mind has been a sore point, the English four grade exam particularly good, poor two points before passing, so the heart has been put down, although the Buddha also advised me several times, do not care too much, but sometimes still have to care about, after all, it is a life experience.

If you have a higher level of UWP, you must have seen it, the wrong file name for your old week. Yes, this name will be run later, it is bound to error, but in order to fool everyone to understand, so I deliberately write the wrong.

So, how to write it? Very easy, in front of the language tag with "lang-", or you can use the full word "language-", the change is like this.

You must ask again, not can not add Lang, why to add it? Yes, there is a specification, which is discussed in two different situations:

1, for the directory, you can not add, you can directly name: ZH-CN, ZH-TW.

2, for the file, you should add Lang, such as: LANG-ZH-CN, Lang-dog-kk.

Because I use only one resource file for this project, I add the language tag directly to the file name, which is to save the oxygen on the earth. If you need to have N resource files for your application project, for example, if you have two resource files for each language, you will have to build the directory, write the language tag on the directory name, and then the same file name in each directory. Just like this.

--ZH-CN

| --RES1.RESW

| --RES2.RESW

--ZH-HK

| --RES1.RESW

| --RES2.RESW

So it's intuitive.

The above is how to arrange the resource file, let's talk about how to enter the resource entry.

After you add the. resw file to your project, you can double-click Open, and VS will open with a. NET resource file editing window. Then, the operation of the head here is the same as the previous. NET project.

For example, the following additions were added to the old week.

Here is the key to the name of the resource, you see, I named above, there is a ".", this point is used to specify the property name. First, you need to make sure that your resource is on which control to apply. Like me here, the resource entry named Demo1 is applied to the TextBox control, so the entire entry is named Demo1. Header, that is, if a TextBox control on the UI references this resource, the value in that resource entry is automatically used to populate the header property of the TextBox control.

The same is true in the second entry, which fills the Placeholdertext property of the TextBox control with the text value in the entry. In the same way, the purpose of DEMO2 is to use the TextBox control.

Demo3 is used on the button control, we all know that the button control has a content property, if a button refers to a DEMO3 resource, then the corresponding resource value will be used to populate the content property of the buttons.

RESW resources can generally populate control properties with content that can be assigned directly as text, except for the properties described above, such as Width, Height, and so on, for example, you can add such a resource entry.

Demo4. Width--700

The name of the resource entry here is Demo4, which sets the width of the control to 700.

Use text content as much as possible, or directly into the value of text, rather than using overly complex content, where complex objects are often not represented by text.

How to reference a resource

The resource file is ready, so how to reference it in XAML code. An extension tag called UID is used here.

Or use the previous example, just now we see, the old week added resource entries for Demo1, Demo2, Demo3, yes, you in the control declaration to reference the resource, you can use the UID directly to specify the name of the resource entry, such as this.

    <x:uid= "/res/demo1"/>    <x:uid = "/res/demo2" />

This reference path is a relative path , and it is written as:/<resw file name >/< resource entry name >

But you should note that when you write a path, the file name extension is not included, and the language region tag name is not included. The. resw file is named Res.lang-zh-hans.resw, so when you write the UID, only the Res is written.

Regardless of whether your resource file is placed in the root directory of the project, or how many layers of subdirectories are nested, the path to the folder is not written, only the name of the resource file is written, and the directory path is ignored. Because of this, the name of the RESW file must be unique throughout the project, because the application is recognized regardless of which directory you are in.

If there is a t.resw,b directory under the A directory and there is a t.resw, it will conflict.

Let's try it out. We built two names of the same resource files, and then placed in different directories (of course, the same directory can not put the same name of the file), and then we respectively in the two files to add an item, are named Item1. As shown in.

At this point, you find that there are two any.resw files, then, press "F5" to run, you will get the following error.

See, repeat.

This means that no matter where the Any.resw file is placed, the application recognizes only the relative path/any.

Now, let's change the item1 in one of the any.resw files to item2. According to this logic, it should be identified as/ANY/ITEM1 and/any/item2, so that estimates are not duplicated.

Sure enough, after this modification, you can compile it smoothly.

How to change language when running

You can set the default language in the app manifest file editor as shown in.

This is generally consistent with the language version of the SDK or VS, in fact, modifying the default language here does not work in the application. For example, I changed to zh-hant and did not display the traditional UI after running.

The application of language and regional priorities is rather complex than dissecting a pig. Is affected by the application settings as well as by the system settings. Microsoft's official documentation, under the "Globalization and Localization" topic below is a table to illustrate the problem, if interested you can see, link please click here.

We have a more important thing, is to specify in the manifest file which language your app supports, of course, you do not do this has no effect, after the old week test, do not specify the language list can also be thrown on the App store.

However, we are still in charge of a good, old week or talk about how to specify the language list. This you want to be an honest child, you have defined several languages in the application of resources, write a few. For example, I just that, I defined Simplified Chinese and traditional Chinese, so I wrote the two brothers.

By right-clicking on the manifest file and executing the "View Code" menu, the manifest editor does not have a relevant location for you to set up, so it is written directly manually.

Find the Resources node, the default is x-generate, this I do not know what the meaning, probably with the compiler language consistent. Because we are going to make a list of the languages for our own application, so this x-generate can be lost.

I should write this in this example application.

  < Resources >    <!-- <resource language= "X-generate"/>  -    <  Language= "zh-hant"/>    <   Language= "Zh-hans"/>  </Resources>

Anyway, you made a few language resources, just write it up OK.

If you don't want to follow the application priorities of a regional language, the best way to set up your app's UI language is to write your own code to work. This law is the most free and can embody the charm of personality.

The following old weeks to introduce some methods, is not all feasible, the old week is not guaranteed. Come on, let's have a try.

Method A:xaml method, the failure rate of this method is about 98%, do not believe? Give it a try.

        <StackPanelMargin= "+"Language= "Zh-hant">            <TextBoxx:uid= "/res/demo1"/>            <TextBoxx:uid= "/res/demo2"/>            <ButtonMargin= "0,13,0,0"x:uid= "/res/demo3"/>        </StackPanel>

We know that XAML inherits many of the attributes of XML, so this Language is no exception. Run, results ...

Oh, still show Simplified Chinese.

Method B: This method has a success rate of 100%, believe it or not. And it's very simple, one line of code.

If dynamic switching is not considered, this is best written on the OnLaunched method of the app.

 if  (e.prelaunchactivated = = false  applicationlanguages.primarylanguageoverride      =  " zh-hant   ;    if  (rootframe.content = =   typeof   (MainPage), e.arguments);  //  Make sure the current window is active   Window.Current.Activate (); }

Actually, that's the line.

Applicationlanguages.primarylanguageoverride = "Zh-hant";

Why write in the code that judges the prelaunchactivated attribute? Don't forget that the UWP has a pre-boot and does not display the UI at pre-boot, so it makes sense to set the language only when the interface needs to be displayed. Of course, this is only applicable to the situation where the resource file is fully used for the UI, and if you want to load something dynamically in the code, you cannot wait for the official launch, which should be loaded at the application entrance.

After such a change, and then run, you will find that the interface is displayed as traditional Chinese.

Because the above line of code will forcibly modify the locale of the current app, it works.

Today, let's talk about how to read resources dynamically in code.

This article sample source download

"Windows 10 app development" elaborate text resource file (RESW)

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.