简介

我正在创建我的第一个Xamarin应用程序(首先是针对UWP,然后是Android,最后可能是iOS) .

基本上,该应用程序需要多种语言:至少英语(默认),法语,西班牙语,俄语和瑞典语 .

我正在尝试添加更少的平台特定代码,因此没有添加特定代码用于本地化 .

我使用UseResxLocaliaztion Xamarin GitHub projectXamarin Localization Documentation .

基本上,我只是复制并尝试使用我的这个项目资源 . 但名称空间是 UsingResxLocalization.Resx ,我的是 WhoStart.Resources ,文件夹名称相同,而不是 Resx .

我更新了 Designer.cs 文件的 ResourceManager 属性,并尝试使用resx属性进行不同的操作,例如更改 GeneratorCustomToolNamespace 没有结果 .

我在某些点上手动更改了 WhoStart.projitems 文件,因为我的VisualStudio没有't indent files and can' t生成 Designer.cs 文件 .

我的环境

  • Windows 10 v1607周年更新(14393.1066)

  • Visual Studio 2017 v15.1(26403.7)

  • Xamarin 4.4.0.34

  • NuGet包:

  • Xamarin.Forms 2.3.4.231

  • Microsoft.NETCore.UniversalWindowsPlatform 5.3.3

问题

尝试访问资源时,我有一个System.Resources.MissingManifestResourceException .

MainPage.xaml.cs

Debug.WriteLine(AppResources.AddButton);

System.Resources.MissingManifestResourceException:'找不到适合指定文化或中性文化的任何资源 . 确保在编译时将“WhoStart.Resources.AppResources.resources”正确嵌入或链接到程序集“WhoStart.UWP”,或者所有所需的附属程序集都是可加载和完全签名的 .

Visual Studio

我觉得它可能来自我的环境:

  • 当我使用Visual Studio向导创建资源文件时,它会创建一个.resw文件 .

  • Visual Studio不会自动生成 Designer.cs 文件

  • 当我打开.resx文件时, Access Modifier 菜单被禁用 .

  • 似乎我没有在论坛上注意到的所有项目属性

  • 右键单击.resx文件时,我没有"Run Custom Tool"

代码

AppResources.resx

<?xml version="1.0" encoding="utf-8"?>
<root>
    <resheader name="resmimetype">
        <value>text/microsoft-resx</value>
    </resheader>
    <resheader name="version">
        <value>2.0</value>
    </resheader>
    <resheader name="reader">
        <value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
    </resheader>
    <resheader name="writer">
        <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
    </resheader>

    <data name="AddButton" xml:space="preserve">
        <value>Add new item</value>
        <comment>this string appears on a button to add a new item to the list</comment>
    </data>
</root>

MainPage.xaml.cs

namespace WhoStart.Resources {
    using System.Reflection;


    /// <summary>
    ///   A strongly-typed resource class, for looking up localized strings, etc.
    /// </summary>
    // This class was auto-generated by the StronglyTypedResourceBuilder
    // class via a tool like ResGen or Visual Studio.
    // To add or remove a member, edit your .ResX file then rerun ResGen
    // with the /str option, or rebuild your VS project.
    [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")]
    [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
    [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
    internal class AppResources {

        private static global::System.Resources.ResourceManager resourceMan;

        private static global::System.Globalization.CultureInfo resourceCulture;

        [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
        internal AppResources() {
        }

        /// <summary>
        ///   Returns the cached ResourceManager instance used by this class.
        /// </summary>
        [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
        internal static global::System.Resources.ResourceManager ResourceManager {
            get {
                if (object.ReferenceEquals(resourceMan, null)) {
                    global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("WhoStart.Resources.AppResources", typeof(AppResources).GetTypeInfo().Assembly);
                    resourceMan = temp;
                }
                return resourceMan;
            }
        }

        /// <summary>
        ///   Overrides the current thread's CurrentUICulture property for all
        ///   resource lookups using this strongly typed resource class.
        /// </summary>
        [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
        internal static global::System.Globalization.CultureInfo Culture {
            get {
                return resourceCulture;
            }
            set {
                resourceCulture = value;
            }
        }

        /// <summary>
        ///   Looks up a localized string similar to Add new item.
        /// </summary>
        internal static string AddButton {
            get {
                return ResourceManager.GetString("AddButton", resourceCulture);
            }
        }
    }
}

WhoStart.projitems

<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <MSBuildAllProjects>$(MSBuildAllProjects);$(MSBuildThisFileFullPath)</MSBuildAllProjects>
    <HasSharedItems>true</HasSharedItems>
    <SharedGUID>2f09a751-1235-4d87-8839-d5828319ad81</SharedGUID>
  </PropertyGroup>
  <PropertyGroup Label="Configuration">
    <Import_RootNamespace>WhoStart</Import_RootNamespace>
  </PropertyGroup>
  <ItemGroup>
    <Compile Include="$(MSBuildThisFileDirectory)App.xaml.cs">
      <DependentUpon>App.xaml</DependentUpon>
    </Compile>
    <Compile Include="$(MSBuildThisFileDirectory)ColorUtil.cs" />
    <Compile Include="$(MSBuildThisFileDirectory)ILocalize.cs" />
    <Compile Include="$(MSBuildThisFileDirectory)MainPage.xaml.cs">
      <DependentUpon>MainPage.xaml</DependentUpon>
    </Compile>
    <Compile Include="$(MSBuildThisFileDirectory)Resources\AppResources.Designer.cs">
      <AutoGen>True</AutoGen>
      <DesignTime>True</DesignTime>
      <DependentUpon>AppResources.resx</DependentUpon>
    </Compile>
    <Compile Include="$(MSBuildThisFileDirectory)Resources\AppResources.es.Designer.cs">
      <AutoGen>True</AutoGen>
      <DesignTime>True</DesignTime>
      <DependentUpon>AppResources.es.resx</DependentUpon>
    </Compile>
    <Compile Include="$(MSBuildThisFileDirectory)Resources\AppResources.fr.Designer.cs">
      <AutoGen>True</AutoGen>
      <DesignTime>True</DesignTime>
      <DependentUpon>AppResources.fr.resx</DependentUpon>
    </Compile>
    <Compile Include="$(MSBuildThisFileDirectory)TranslateExtension.cs" />
  </ItemGroup>
  <ItemGroup>
    <EmbeddedResource Include="$(MSBuildThisFileDirectory)MainPage.xaml">
      <SubType>Designer</SubType>
      <Generator>MSBuild:UpdateDesignTimeXaml</Generator>
    </EmbeddedResource>
    <EmbeddedResource Include="$(MSBuildThisFileDirectory)App.xaml">
      <SubType>Designer</SubType>
      <Generator>MSBuild:UpdateDesignTimeXaml</Generator>
    </EmbeddedResource>
    <EmbeddedResource Include="$(MSBuildThisFileDirectory)Resources\AppResources.es.resx">
      <Generator>PublicResXFileCodeGenerator</Generator>
      <LastGenOutput>AppResources.es.Designer.cs</LastGenOutput>
      <CustomToolNamespace>WhoStart.Resources</CustomToolNamespace>
    </EmbeddedResource>
    <EmbeddedResource Include="$(MSBuildThisFileDirectory)Resources\AppResources.fr.resx">
      <Generator>PublicResXFileCodeGenerator</Generator>
      <LastGenOutput>AppResources.fr.Designer.cs</LastGenOutput>
      <CustomToolNamespace>WhoStart.Resources</CustomToolNamespace>
    </EmbeddedResource>
    <EmbeddedResource Include="$(MSBuildThisFileDirectory)Resources\AppResources.resx">
      <Generator>PublicResXFileCodeGenerator</Generator>
      <LastGenOutput>AppResources.Designer.cs</LastGenOutput>
      <CustomToolNamespace>WhoStart.Resources</CustomToolNamespace>
    </EmbeddedResource>
  </ItemGroup>
</Project>

研究