C# 更改资源文件 (resx) 命名空间和访问修饰符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1333356/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 15:15:29  来源:igfitidea点击:

Changing Resource files (resx) namespace and access modifier

c#visual-studio-2008namespacesresx

提问by Michael Pereira

In my webproject I'm using 4 resources files in my App_GlobalResourcesfolder. One of them (lang.resx) has been created before my arrival on the project. It has the correct namespace (WebApplication.App_GlobalResources) and access modifier : public.

在我的 web 项目中,我在我的App_GlobalResources文件夹中使用了 4 个资源文件。其中之一 ( lang.resx) 已在我到达项目之前创建。它具有正确的命名空间 ( WebApplication.App_GlobalResources) 和访问修饰符 : public

On the other hand, the three others Resource files that I just created have a different namespace (Resources) and internalaccess modifier, and I can't change it on the Resource File Form from Visual Studio because it's disabled. If I try to change it directly in the designer.cs file, the modifications are cancelled on the next save of the file.

另一方面,我刚刚创建的其他三个资源文件具有不同的命名空间 ( Resources) 和internal访问修饰符,我无法在 Visual Studio 的资源文件表单上更改它,因为它已被禁用。如果我尝试直接在 Designer.cs 文件中更改它,则在下次保存文件时会取消修改。

It's not a critical bug but it can be misleading for the others developers on the project to find different namespaces and access modifiers for the resources files they will use.

这不是一个严重的错误,但它可能会误导项目中的其他开发人员为他们将使用的资源文件找到不同的命名空间和访问修饰符。

采纳答案by Dave Van den Eynde

I'm not entirely sure where the problem lies yet, but I can tell you that you can solve it by changing the tool used to generate the code.

我还不完全确定问题出在哪里,但我可以告诉您,您可以通过更改用于生成代码的工具来解决它。

When I tried to follow this article, I also stumbled onto this problem. After downloading the source files as the author suggested, I noticed that the resource file that were already present had the following class in the "Custom Tool" property: "PublicResXFileCodeGenerator". Also, the "Build Action" property was set to "Embedded Resource", but I'm not sure if that's part of the problem.

当我尝试关注这篇文章时,我也偶然发现了这个问题。按照作者的建议下载源文件后,我注意到已经存在的资源文件在“自定义工具”属性中具有以下类:“PublicResXFileCodeGenerator”。此外,“构建操作”属性设置为“嵌入式资源”,但我不确定这是否是问题的一部分。

Any new resource file that I created used the custom tool "GlobalResourceProxyGenerator". After overwriting this with the aforementioned "PublicResXFileCodeGenerator" seemed to solve the problem, whatever the real problem may be.

我创建的任何新资源文件都使用了自定义工具“GlobalResourceProxyGenerator”。用前面提到的“PublicResXFileCodeGenerator”覆盖后似乎解决了问题,不管真正的问题是什么。

I also noticed that the present resource file was in the "2.0" format, whereas new files were in the "1.3" format. You can see this when you open the resx file using an XML editor (or by using "open with" from visual studio itself).

我还注意到当前的资源文件是“2.0”格式,而新文件是“1.3”格式。当您使用 XML 编辑器(或使用 Visual Studio 本身的“打开方式”)打开 resx 文件时,您可以看到这一点。

I hope you can make it work like this, it's not ideal though. It's likely to be an installation issue with Visual Studio 2008 and SP1, or something like that.

我希望你可以让它像这样工作,但这并不理想。这可能是 Visual Studio 2008 和 SP1 或类似问题的安装问题。

Update:

更新:

This blog entrymay also help.

此博客条目也可能有所帮助。

回答by psychotik

The resx picks up the namespace depending on the namespace specified in your Visual Studio project configuration. Update your project to have the right namespace, and the resx should inherit it (new ones for sure, not sure if existing ones will be fixed - they should).

resx 根据 Visual Studio 项目配置中指定的命名空间选取命名空间。更新您的项目以拥有正确的命名空间,并且 resx 应该继承它(肯定是新的,不确定是否会修复现有的 - 他们应该)。

回答by Luis Filipe

Resource Files access modifiers are in the .csproj;

资源文件访问修饰符位于.csproj;

Changing directly the .csprojfile should workaround this problem.

直接更改.csproj文件应该可以解决此问题。

Look for the <Generator>element and set its value in accordance to the examples below:

<Generator>根据以下示例查找元素并设置其值:

A resource file with internalmodifier looks like this,

带有internal修饰符的资源文件如下所示,

<ItemGroup>
 <EmbeddedResource Update="resources.resx">
  <Generator>ResXFileCodeGenerator</Generator>
  <LastGenOutput>resources.Designer.cs</LastGenOutput>
 </EmbeddedResource>
</ItemGroup>

where a resource file with publicmodifier looks like this.

带有public修饰符的资源文件如下所示。

<ItemGroup>
 <EmbeddedResource Update="resources.resx">
  <Generator>PublicResXFileCodeGenerator</Generator>
  <LastGenOutput>resources.Designer.cs</LastGenOutput>
 </EmbeddedResource>
</ItemGroup>

回答by Joost Schepel

Or you can change the CustomTool attribute (tested in VS2010).

或者您可以更改 CustomTool 属性(在 VS2010 中测试)。

You just have to open the file properties of the resource file and change the “Custom Tool” from “GlobalResourceProxyGenerator” to “PublicResXFileCodeGenerator”, which is the default Tool for local resource files. Next you have to change the “Build Action” to “Embedded Resource”. You may also want to assign a proper Custom Tool Namespace like “Resources” in order to access the file properly, but this isn't necessary...

您只需要打开资源文件的文件属性,将“自定义工具”从“GlobalResourceProxyGenerator”更改为“PublicResXFileCodeGenerator”,这是本地资源文件的默认工具。接下来,您必须将“构建操作”更改为“嵌入式资源”。您可能还想分配一个适当的自定义工具命名空间,如“资源”,以便正确访问文件,但这不是必需的...

Source

来源

回答by Mark Bonafe

The quick answer is: Just open the Properties of the resource file and change the Custom Tool Namespaceto the namespace you need.
Simple as that.

快速回答是:只需打开资源文件的属性并将其更改为Custom Tool Namespace您需要的命名空间。
就那么简单。