How to use "resx" in a .NET MAUI library?

Wojciech Kulik 20 Reputation points
2024-07-18T20:16:35.32+00:00

Hello,

I migrated my shared Xamarin project to .NET MAUI Library. I need to use translations that are stored there. They need to be accessible from this library and from the MAUI app project that references this library.

How can I configure my csproj to generate strongly typed properties for the resx file? I couldn't find any information on that. Tutorial only says to put this file in MAUI app project. However, then it wouldn't be accessible by my modules inside .NET MAUI Library (migrated Xamarin shared project)?

So far I figured out that I have to rename the previously generated file Strings.Designer.cs to Strings.cs and replace internal with public. However, this is just a workaround, because the new translations are not auto-generated.

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,460 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 42,351 Reputation points Microsoft Vendor
    2024-07-19T03:24:12.4166667+00:00

    Hello,

    After testing, using Resx files in MAUI's Class Library is basically the same as the steps in this document. All you need to do is set the Access Modifier for the basic resource file to public.

    After that, you can refer to the following code to reference the resource files in the library.

    <ContentPage xmlns="
                 xmlns:local="clr-namespace:MauiLib1;assembly=MauiLib1">
    
    <Label
        Text="{x:Static local:AppResources.AddButton}"
        Style="{StaticResource Headline}"
        SemanticProperties.HeadingLevel="Level1" />
    

    Best Regards,

    Alec Liu.


    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.


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.