Konrad
Konrad

Reputation: 4812

Using Resource-Files for Strings in Xamarin

I want to use a resource file for all my strings of my Xamarin app - since I want the same strings for all my 3 platforms (iOS, Android, UWP) I created a new resx file in the Xamarin-Forms project. But the file isn't handled as expected by Visual Studio ... the matching .cs file for the resources is not build. But is this necessary at all?

I tried several solutions:

var resourceLoader = new ResourceLoader();
label1.Text = resourceLoader.GetString("String1");

as well as

ResourceManager rm =
    new ResourceManager("MyApp.Resources1", typeof(MainPage).GetTypeInfo().Assembly);
label1.Text = rm.GetString("String1");

But at runtime (UWP) I got the error:

Could not find any resources appropriate for the specified culture or the neutral culture.

And that should be only the first step - later I hope to bind the strings from the resources in XAML directly to the controls.

But I assume, I'm on a totally wrong way ... and there is no platform independent way to use string resources ... ?

Upvotes: 2

Views: 9855

Answers (2)

Shenghung Lin
Shenghung Lin

Reputation: 1

EvZ points to the correct answer - Localizing Xamarin.Forms Apps with RESX Resource Files

I tested the Android sample project, and it crashed on Xamarin Live Player 1.2.80. However, the code worked fine on a physical Android device.

To add shared-RESX files, the project must be PCL or .NET Standard. SAP projects add only RESW files.

Upvotes: 0

jack_tux
jack_tux

Reputation: 429

You need to modify the Access Level on the Resources to Public

Access Modifier

Then you can just use

var s1 = Resource1.ResourceManager.GetString("String1");

anywhere in any project

Upvotes: 3

Related Questions