0

I followed these instructions to upgrade my Xamarin.Forms project to .NETStandard 2.0. Now I added a new entry to my .resx file and get a compile error:

error CS0117: 'AppResources' does not contain a definition for 'MyKey'

I think the AppResources.Designer.cs file is not updated anymore. The following things seems to be wrong:

  • According to the instructions I should delete AssemblyInfo.cs, which I have done only for the PCL and not the indivdual projects (iOS, Droid). On the other side you should add NeutralResourcesLanguage to the AssemblyInfo.cs according to the docs. So should I have a AssemblyInfo.cs or not?
  • I cannot change the access modifier if I open AppResources.resx (works after manipulating .csproj)
  • I only have this code in my new .csproj of the PCL

    <ItemGroup>
        <EmbeddedResource Update="Common\Localization\AppResources.resx">
            <Generator>ResXFileCodeGenerator </Generator>
        </EmbeddedResource>
    </ItemGroup>
    
  • In the detailed build logs I found this and I don't know what I should do with this

1> Task "CreateCSharpManifestResourceName" skipped, due to false condition; ('%(EmbeddedResource.ManifestResourceName)' == '' and '%(EmbeddedResource.WithCulture)' == 'true' and '%(EmbeddedResource.Type)' == 'Non-Resx') was evaluated as ('' == '' and 'false' == 'true' and 'Resx' == 'Non-Resx'). 1> Task "CreateCSharpManifestResourceName" skipped, due to false condition; ('%(EmbeddedResource.ManifestResourceName)' == '' and '%(EmbeddedResource.WithCulture)' == 'true' and '%(EmbeddedResource.Type)' == 'Non-Resx') was evaluated as ('' == '' and 'true' == 'true' and 'Resx' == 'Non-Resx'). 1> Task "CreateCSharpManifestResourceName" skipped, due to false condition; ('%(EmbeddedResource.ManifestResourceName)' == '' and '%(EmbeddedResource.WithCulture)' == 'true' and '%(EmbeddedResource.Type)' == 'Non-Resx') was evaluated as ('' == '' and 'false' == 'true' and 'Non-Resx' == 'Non-Resx').

My best bet would be to manually edit the .csproj from the PCL project. In my old .csproj I had this:

<Compile Include="Common\Localization\AppResources.Designer.cs">
    <AutoGen>True</AutoGen>
    <DesignTime>True</DesignTime>
    <DependentUpon>AppResources.resx</DependentUpon>
</Compile>

<ItemGroup>
    <EmbeddedResource Include="Common\Localization\AppResources.de.resx">
        <SubType>Designer</SubType>
    </EmbeddedResource>
    <EmbeddedResource Include="Common\Localization\AppResources.resx">
        <Generator>ResXFileCodeGenerator</Generator>
        <LastGenOutput>AppResources.Designer.cs</LastGenOutput>
        <SubType>Designer</SubType>
    </EmbeddedResource>
</ItemGroup>

But I don't think I can adopt it one-to-one. I tried to play with it, but didn't had success:

<ItemGroup>
    <Compile Update="Common\Localization\AppResources.Designer.cs">
        <AutoGen>True</AutoGen>
        <DesignTime>True</DesignTime>
        <DependentUpon>AppResources.resx</DependentUpon>
    </Compile>
</ItemGroup>
<ItemGroup>
    <EmbeddedResource Update="Common\Localization\AppResources.de.resx">
        <SubType>Designer</SubType>
    </EmbeddedResource>
    <EmbeddedResource Update="Common\Localization\AppResources.resx">
        <Generator>ResXFileCodeGenerator</Generator>
        <LastGenOutput>AppResources.Designer.cs</LastGenOutput>
        <SubType>Designer</SubType>
    </EmbeddedResource>
</ItemGroup>

How do I get my translation working again?

testing
  • 19,681
  • 50
  • 236
  • 417
  • Are you trying to embed your resources files in shared code? – Adlorem Dec 06 '19 at 19:36
  • @Adlorem: The resources are in the Portable Class Library (PCL), which is shared with the other projects (OS specific ones: iOS, Android). – testing Dec 06 '19 at 20:37

1 Answers1

0

Seems the only thing that was needed is to set the Custom Tool option of AppResources.resx to PublicResXFileCodeGenerator. This results into the following .csproj code:

<ItemGroup>
    <Compile Update="Common\Localization\AppResources.Designer.cs">
        <AutoGen>True</AutoGen>
        <DesignTime>True</DesignTime>
        <DependentUpon>AppResources.resx</DependentUpon>
    </Compile>
</ItemGroup>
<ItemGroup>
    <EmbeddedResource Update="Common\Localization\AppResources.de.resx">
        <SubType>Designer</SubType>
    </EmbeddedResource>
    <EmbeddedResource Update="Common\Localization\AppResources.resx">
        <Generator>PublicResXFileCodeGenerator</Generator>
        <LastGenOutput>AppResources.Designer.cs</LastGenOutput>
        <SubType>Designer</SubType>
    </EmbeddedResource>
</ItemGroup>

The Access Modifier for the individual key/value pairs of AppResources.resx now is Public. All other language resource files have the Acces Modifier of No code generation.

I also deleted the other AssemblyInfo.cs files as it seems that it works without them and without NeutralResourcesLanguage as opposed to the sample in the docs. Nevertheless, I set the Assembly netural language under Package in the project properties. Therefore the .csproj now contains an <NeutralLanguage>en</NeutralLanguage> entry.

What I find odd is that you can't add a Resource file in Visual Studio anymore ...

testing
  • 19,681
  • 50
  • 236
  • 417