【发布时间】:2016-09-05 03:58:09
【问题描述】:
我正在尝试在可移植类中包含一个资源文件,根据 Microsoft (App Resources for Libraries That Target Multiple Platforms) 的这篇文章,我应该将 Access Modifier 设置为 public:
要在 Visual Studio 中创建强类型包装器,请将 Visual Studio 资源设计器中的主资源文件的访问修饰符设置为 Public。这将创建一个包含强类型 ResourceManager 包装器的 [resourceFileName].designer.cs 或 [resourceFileName].designer.vb 文件。
然后我应该能够使用应该创建的包装类直接访问资源。我的资源文件名为 SharedResources.en-US.resx; SharedResources.fr-FR.resx 等...
所以我应该能够通过调用以下包装类来访问它:
string str = SharedResources.UnsupportedFeature;
但它不会为资源文件创建任何包装文件,即使在我从Internal 转到Public 之后也是如此。我想可能是因为我将它们存储在 Strings 文件夹中,所以我将各种资源文件移动到项目的根目录,但没有任何区别。
有人对我如何解决这个问题有任何想法或建议吗?
谢谢。
更新 1:
奇怪,我注意到当我创建一个刚刚叫 Resources.resx 的新资源文件时,它会在 Resources.Designer.cs 中创建以下内容:
namespace MyCompany.MyApp.Shared.Strings {
using System;
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 Resources {
private static global::System.Resources.ResourceManager resourceMan;
private static global::System.Globalization.CultureInfo
.resourceCulture;
[global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute
("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
internal Resources() {
}
/// <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
("MyCompany.MyApp.Shared.Strings.Resources",
typeof(Resources).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 Unsupported
/// feature provided.
/// </summary>
internal static string UnsupportedFeature {
get {
return ResourceManager.GetString("UnsupportedFeature",
resourceCulture);
}
}
}
}
- 如果我重命名文件,此代码将被完全删除。
- 如果我创建一个名为 Resources.fr-FR.resx 的文件,则不会创建代码。
我会玩弄上面的代码,看看能不能成功。
【问题讨论】:
标签: c# visual-studio localization embedded-resource portable-class-library