如何有不同的解决方案是指一个RESX文件? [英] How to have different solutions refer to one resx file?

查看:427
本文介绍了如何有不同的解决方案是指一个RESX文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个RESX文件,我想从几个解决方案\项目中使用它,我不希望有在每个解决方案的本地副本(只在编译时带上复印件)。

I have one resx file and I want to use it from several solutions\projects , and i don't want to have local copy at each solution(only at compile time bring a copy).

有没有一些方法来做到这一点?因为当我将其添加为添加现有文件将其复制我一份本地。

Is there some way to do so ? Because when i add it as "Add existing file" it copies me a copy locally.

任何想法如何做到这一点?

Any idea how to do so ?

推荐答案

如果您使用的是.resx文件,你可能想利用自动生成的code功能,Visual Studio提供了的.resx文件。如果你包括多个项目中的.resx文件,那么你可能希望有每个项目自动生成它自己的code。因为你要使用某些项目不同的code发生器(的ResX codeFileGenerator与GlobalResourceProxyGenerator),或者你可能只是想的产生code中的命名空间与项目相一致,你可以这样做。这里是你如何设置了。

If you're using a .resx file, you probably want to take advantage of the auto-generated code functionality that Visual Studio provides for .resx files. If you're including a .resx file in multiple projects, then you may want to have each project auto-generate its own code. You might do this because you want to use a different code generator for certain projects (ResXCodeFileGenerator vs. GlobalResourceProxyGenerator) or you might just want the namespace of the generated code to be aligned with the project. Here's how you can set that up.

我已经成立了一个名为SharedResx C#控制台应用程序项目一个新的解决方案。我还添加了一个C#类库项目名为资源解决方案,并在项目中我添加了一个名为MySharedResource.resx一个新的资源文件。 Visual Studio会自动在我的资源项目的资源命名空间内的code创建一个MySharedResource.Designer.cs文件。

I've set up a new solution with a C# console application project called SharedResx. I've also added a C# class library project to the solution called Resources, and within that project I added a new resource file named MySharedResource.resx. Visual Studio automatically creates a MySharedResource.Designer.cs file within my Resources project with the code inside of the Resources namespace.

首先,请在SharedResx控制台应用程序项目为纽带,现有的.resx文件,因为其他的答案也提到。添加现有项 - >找到.. \资源\ MySharedResource.resx - >使用下拉Add按钮列表中选择添加为链接

First, include your existing .resx file in the SharedResx console application project as a link, as other answers have mentioned. Add Existing Item -> find ..\Resources\MySharedResource.resx -> use the drop down list on the Add button to select "Add as Link".

接下来,您必须手动修改项目文件来设置自动生成。你可以看一下Resources.csproj看到自动生成的设置方式也为它应该是什么样子的SharedResx.csproj的指南。 在SharedResx项目上单击右键,选择卸载项目。再次单击鼠标右键,然后选择编辑SharedResx.csproj。 向下滚动找到对应于您的链接.resx文件的EmbeddedResource元素:

Next, you'll have to manually modify the project file to set up auto-generation. You can look at the Resources.csproj to see how the auto-generation is set up there as a guide for how it should look in SharedResx.csproj. Right-click on the SharedResx project and select Unload Project. Right-click again and select Edit SharedResx.csproj. Scroll down to find the EmbeddedResource element that corresponds to your linked .resx file:

<EmbeddedResource Include="..\Resources\MySharedResource.resx">
  <Link>MySharedResource.resx</Link>
</EmbeddedResource>

修改这包括发电机元件和LastGenOutput元素:

Modify this to include a Generator element and a LastGenOutput element:

<EmbeddedResource Include="..\Resources\MySharedResource.resx">
  <Link>MySharedResource.resx</Link>
  <Generator>ResXFileCodeGenerator</Generator>
  <LastGenOutput>SharedResx.MySharedResource.Designer.cs</LastGenOutput>
</EmbeddedResource>

请注意:如果你想控制生成的文件名,因为我在我的例子所做的手动编辑才是必需的。否则,您可以指定使用自定义工具设置在Visual Studio属性窗口中生成的元素。

Note: manual editing is only necessary if you want to control the name of the generated file as I've done in my example. Otherwise, you can specify the Generator element using the Custom Tool setting in the Properties window in Visual Studio.

另外请注意,在LastGenOutput元素我命名的SharedResx项目名称为preFIX生成的文件。这将导致ResXFile codeGenerator创建一个名为文件相同的文件夹中.resx文件SharedResx.MySharedResource.Designer.cs。我已经尝试过在LastGenOutput元素,以获得所生成的文件是在不同的文件夹,如SharedResx文件夹指定一个相对路径,而不是仅仅一个文件名,但我发现它不工作始终。虽然我能够生成文件在正确的位置的第一次,LastGenOutput元素失去了价值,使后代不会针对同一个位置。我放弃了这一点,只是使用的项目名称preFIX作为文件名,以避免与其他项目可能发生的冲突的部分。

Also note that in the LastGenOutput element I've named the generated file with the SharedResx project name as a prefix. This will cause the ResXFileCodeGenerator to create a file named SharedResx.MySharedResource.Designer.cs in the same folder as the .resx file. I've experimented with specifying a relative path rather than just a file name in the LastGenOutput element in order to get the generated file to be in a different folder, such as the SharedResx folder, but I found that it didn't work consistently. While I was able to generate the file in the correct location the first time, the LastGenOutput element lost its value so that subsequent generations would not target that same location. I gave up on that and just used the project name prefix as part of the file name in order to avoid possible conflicts with other projects.

现在,关闭SharedResx.csproj文件,再次右键单击,选择刷新项目。就在SharedResx项目链接MySharedResource.resx文件上单击右键并选择运行自定义工具。您现在应该看到名为SharedResx.MySharedResource.Designer.cs一个新的链接文件被添加到项目作为MySharedResource.resx文件下嵌套的文件。您可能需要打开在解决方案资源管理器窗口中的显示所有文件选项,才能看到它。

Now, close the SharedResx.csproj file and right-click again to select Reload Project. Right-click on the linked MySharedResource.resx file in the SharedResx project and select Run Custom Tool. You should now see that a new linked file named SharedResx.MySharedResource.Designer.cs was added to the project as a nested file under the MySharedResource.resx file. You may have to turn on the "Show All Files" option in the Solution Explorer window in order to see it.

您现在有code文件从共享的.resx文件自动生成包含在你的项目中。

You now have a code file auto-generated from your shared .resx file included in your project.

这篇关于如何有不同的解决方案是指一个RESX文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆