How to fetch a string resource in a class library

Alex 6 Reputation points
2019-12-10T12:11:50.953+00:00

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

Universal Windows Platform (UWP)
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Xiaodi Yan 876 Reputation points MVP
    2019-12-10T19:54:17.947+00:00

    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 person found this answer helpful.

  2. Alex 6 Reputation points
    2019-12-11T09:24:33.007+00:00

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

    1 person found this answer helpful.

  3. Alex 6 Reputation points
    2019-12-11T02:52:24.84+00:00

    @Xiaodi Yan

    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.

    0 comments No comments