question

Alex-2663 avatar image
0 Votes"
Alex-2663 asked ·

How to fetch a string resource in a class library

Hi all,

I'm working on a class library which contains several .resw resource files and pages. The question is, how to fetch the string for UI component properties.

For example, I have a class library called "lib", which has a resource file UIResource.resw which has a resource "Button1.Content", the value for "Button1.Content" is "OK". There is also a page in the class library, named "Page1" which has a button named "Button1", now in Page1.xaml, if I'd like to use "OK" for Button1's content, how should I process?

Note: the button may be in a datatemplate in a GridView, so fetch the resource in C# code may not be a good idea.

Did I make it clear enough? If not, please feel free to ask. Any help would be appreciated, thank you.

Alex

windows-uwpwindows-uwp-xaml
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

yanxiaodi avatar image
1 Vote"
yanxiaodi answered ·

You can use x:Uid to associate the control id with the string resource identifier:


 < Button x:Name="Button1" x:Uid="Button1" />


FYI: localize-strings-ui-manifest

· 1 · Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks for the reply. But sorry, this won't work. Actually this is what I'm doing now. Let me try to make it more clear in another way. I got a solution with multiple projects. Two of them are: UWPProject --is an uwp project, the start up project. Relies on LibProject . LibProject --is a class library. Containing a resw resource file UIResource.resw, and a page class Page1.

What I'm going to achieve, is to fetch a defined string resource Button1.Content from UIResource.resw, and use it for a button control in Page1. Here is what I'm doing now:

alt text

In this case, the Content property of Button1, is always an empty string. What I can guess is that, the application was trying to fetch the string resource from UWPProject which is the start up project and different than LibProject. So how can I fetch the resource in LibProject?


0 Votes 0 · ·
Alex-2663 avatar image
0 Votes"
Alex-2663 answered ·

@yanxiaodi

Thanks for replying. But sorry, no, this won't work. Actually this is what I'm doing now. Let me try to make it more clear in another way.
I got a solution with multiple projects. Two of them are:
UWPProject --which is a uwp project, the start up project. Relies on LibProject .
LibProject --which is a class library. Containing a resw resource file UIResource.resw, and a page class Page1.

What I'm going to achieve, is to fetch a defined string resource Button1.Content from UIResource.resw, and use it for a button control in Page1. Here is what I'm doing now:

alt text

In this case, the Content property of Button1, is always an empty string. What I can guess is that, the application was trying to fetch the string resource from UWPProject which is the start up project and different than LibProject. So how can I fetch the resource in LibProject?

Note again: the button may be in a datatemplate in a GridView, so fetch the resource in C# code may not be a good idea.



3.png (2.9 KiB)
· Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Alex-2663 avatar image
1 Vote"
Alex-2663 answered ·

Problem solved. Use following code to access resource in a class lib:alt text



4.png (3.2 KiB)
· 1 · Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

That's great!�� It's similar with the syntax when we reference a style resource file.

0 Votes 0 · ·