-1

I try to get to the resource string of the resx file all day. I try this way:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         x:Class="test.MainPage">

<StackLayout>
    <Label Text="{x:Static res:AppResources.String1}"/>
</StackLayout>

</ContentPage>

I created an AppResources file in directly in the test.android project with a String1 resource and set in public. How can I get to this resource on xaml? I have tried various methods found on the internet and nothing works.

  • 1
    you have not specified what the "res" prefix is. This is covered in the official docs: https://learn.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/localization/text?pivots=macos#localize-text-in-xamarinforms – Jason Jan 11 '22 at 20:48

2 Answers2

1

You need to define the res namespace for the resx file and add it to the top of your page:

<ContentPage 
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:res="clr-namespace:YourResxFile.Namespace"
    x:Class="test.MainPage">
    

More info on localization can be found in Xamarin.Forms String and Image Localization

In your case you could use: xmlns:res="clr-namespace:test"

haldo
  • 14,512
  • 5
  • 46
  • 52
0

I created an AppResources file in directly in the test.android project with a String1 resource and set in public. How can I get to this resource on xaml?

Create a Xamarin.Forms project and then add the AppResources.resx in your Forms project directly. I created a App1 project for reference.

enter image description here

Xaml: Define the res.

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:res="clr-namespace:App1"
         x:Class="App1.Page3">
<ContentPage.Content>
    <StackLayout>
        <Label Text="{x:Static res:AppResources.String1}"/>
    </StackLayout>
</ContentPage.Content>
</ContentPage>

Set the App1.Android as Startup project to run.

If you want to create a folder to put the .resx files, you could define the resx like below.

xmlns:res="clr-namespace:App1.folderName"
Wendy Zang - MSFT
  • 10,509
  • 1
  • 7
  • 17