How do I use resource files in a WinUI project

Gavin Williams 761 Reputation points
2025-02-06T03:09:25.4+00:00

I'm trying to use the resource system in VS, but when I try to access a resource, it can't find it. Here's my setup...

  • I have a WinUI project with a library
    DemoApp
    GfxToolkit
  • In the GfxToolkit library project I have a resources file
    Resources.resw

When I open the Resource Explorer I see:
GfxToolkit
[ ] Properties\Resources
[✓] Resources
In the selected Resources collection, I have a single resource:
TextureRendererVS

In code, I construct the Resource Manager with
ResourceManager = new ResourceManager("GfxToolkit.Resources", Assembly.GetExecutingAssembly());

I attempt to access the resource with
string? source = (string?)ResourceManager.GetObject("TextureRendererVS") ?? throw new Exception();

I get an error
MissingManifestResourceException: Could not find the resource "GfxToolkit.Resources.resources" among the resources

What am I doing wrong?

Windows App SDK
Windows App SDK
A set of Microsoft open-source libraries, frameworks, components, and tools to be used in apps to access Windows platform functionality on many versions of Windows. Previously known as Project Reunion.
821 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,279 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Roy Li - MSFT 33,851 Reputation points Microsoft Vendor
    2025-02-06T06:44:14.63+00:00

    Hello,

    Welcome to Microsoft Q&A!

    I tried to make a sample which could reproduce your scenario based on your description. An app with a class library.

    User's image

    In such scenario, if you want to get resource from other packages, you will need to accessed through the package's own top-level ResourceMap that's accessible from the ResourceManager.

    So you could try the following code instead of creating a ResourceManager with a specific file:

     private void myButton_Click(object sender, RoutedEventArgs e)
     {
         var resourceManager = new Microsoft.Windows.ApplicationModel.Resources.ResourceManager();
        
         var str = resourceManager.MainResourceMap.GetValue("TestLibrary/Resources/TextureRendererVS").ValueAsString;
    
         myButton.Content = str;
     }
    
    

    You could get more information here: Loading strings from other packages and ResourceMap Class

    Thank you.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.