更改资源文件 (resx) 命名空间和访问修饰符
在我的 web 项目中,我在 App_GlobalResources
文件夹中使用了 4 个资源文件。其中之一 (lang.resx
) 在我到达该项目之前就已创建。它具有正确的命名空间 (WebApplication.App_GlobalResources
) 和访问修饰符:public
。
另一方面,我刚刚创建的其他三个资源文件具有不同的命名空间 (Resources
) 和 internal
访问修饰符,并且我无法在资源上更改它来自 Visual Studio 的文件表单,因为它已被禁用。如果我尝试直接在 Designer.cs 文件中更改它,则修改将在下次保存文件时取消。
这不是一个严重的错误,但它可能会误导项目中的其他开发人员为他们将使用的资源文件找到不同的命名空间和访问修饰符。
In my webproject I'm using 4 resources files in my App_GlobalResources
folder. 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
.
On the other hand, the three others Resource files that I just created have a different namespace (Resources
) and internal
access 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.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
简单的答案是:只需打开资源文件的属性并将
自定义工具命名空间
更改为您需要的命名空间。就这么简单。
The quick answer is: Just open the Properties of the resource file and change the
Custom Tool Namespace
to the namespace you need.Simple as that.
我还不完全确定问题出在哪里,但我可以告诉你,你可以通过更改用于生成代码的工具来解决它。
当我尝试遵循这篇文章时,我也偶然发现了这个问题。按照作者的建议下载源文件后,我注意到已经存在的资源文件在“自定义工具”属性中具有以下类:“PublicResXFileCodeGenerator”。另外,“构建操作”属性设置为“嵌入式资源”,但我不确定这是否是问题的一部分。
我创建的任何新资源文件都使用自定义工具“GlobalResourceProxyGenerator”。用前面提到的“PublicResXFileCodeGenerator”覆盖它后似乎解决了问题,无论真正的问题是什么。
我还注意到当前的资源文件采用“2.0”格式,而新文件采用“1.3”格式。当您使用 XML 编辑器(或使用 Visual Studio 本身的“打开方式”)打开 resx 文件时,您可以看到这一点。
我希望你能让它像这样工作,但这并不理想。这可能是 Visual Studio 2008 和 SP1 或类似问题的安装问题。
更新:
此博客条目也可能有所帮助。
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.
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.
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).
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.
Update:
This blog entry may also help.
或者您可以更改CustomTool属性(在VS2010中测试)。
来源
Or you can change the CustomTool attribute (tested in VS2010).
Source
resx 根据 Visual Studio 项目配置中指定的命名空间来选择命名空间。更新您的项目以拥有正确的命名空间,并且 resx 应该继承它(当然是新的,不确定现有的是否会被修复 - 它们应该)。
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).
资源文件访问修饰符位于
.csproj
中;直接更改 .csproj 文件应该可以解决此问题。
查找
元素并根据以下示例设置其值:带有
internal
修饰符的资源文件如下所示,其中带有
的资源文件>public
修饰符看起来像这样。Resource Files access modifiers are in the
.csproj
;Changing directly the
.csproj
file should workaround this problem.Look for the
<Generator>
element and set its value in accordance to the examples below:A resource file with
internal
modifier looks like this,where a resource file with
public
modifier looks like this.