在Visual Studio中跨项目共享WinForms资源的问题 [英] Issue with shared WinForms resources across projects in Visual Studio

查看:105
本文介绍了在Visual Studio中跨项目共享WinForms资源的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经将大型多项目解决方案中的所有图像资源移到了专门为共享资源创建的新项目中.

I've moved all image resources in a large multi-project solution to a new project specifically created for shared resources.

向使用共享图像的其他项目添加新引用按预期进行.下方所选按钮中的图像继续出现在设计器中,但是有问题.

Adding a new reference to other projects where the shared images are used is working as expected. The image in the selected button below continues to appear in designer but with issues.

在VS Designer中,我无法在图像编辑器"中选择共享资源,而不得不手动编辑设计器代码.

In VS Designer, I'm unable to select shared resources in the Image Editor, having instead to manually edit the designer code.

更具体地说,在图像编辑器"对话框(可从现有控件的图像"属性访问)中,选择从文件/资源​​中选择..."以弹出选择资源"对话框(如图所示).我曾经访问过所有本地项目资源(Resources.resx),现在我想添加对新共享资源项目的访问权限,理想情况下,我将在显示的下拉菜单中添加第二项,该选项切换到共享资源项目的resx

More specifically, in the Image Editor dialog (accessed from the 'Image' property of an existing control) I select 'Select from File/Resource..." to pop up the Select Resource dialog (shown). There, where I used to access all local project resources (Resources.resx), I would like now to add access to the new shared resources project. Ideally, I would add a second item to the dropdown shown which switches to the resx for the shared resources project.

这怎么办?有更好的方法吗?

How can this be done? Is there a better way?

推荐答案

我能够在项目之间共享一个resx文件,并在VS 2008 SP1中的设计器中使用它(不知道其他版本).与其他方法不同,这里的资源是静态和动态共享的,因此在运行时实际上是共享资源的一个副本.不会造成存储或内存浪费,并且易于进行大量项目的维护.

I was able to share a resx file between project and using it in the designer in VS 2008 SP1 (dunno other versions). Differently from other approaches, here resources are statically and dynamically shared, so at runtime there's really one copy of the shared resources. No storage or memory waste and it's easy to maintain with tons of projects.

按照指南进行操作,并告诉我它是否对您有用:

Follow the guide and tell me if it works for you:

  • 创建课程项目资源;
  • 创建资源文件(转到属性"->项目资源);
  • 将资源文件(默认为Resources.resx)移动到项目的
  • 重命名资源文件Resources.resx-> GlobalResources.resx;
  • 将资源文件修改器修改为公共"(双击资源文件访问修改器"->公共");
  • 将任何资源文件复制到项目的根目录
  • 使用添加现有文件" 并在项目的 root 目录中选择资源,将任何资源添加到资源文件中.
  • Create a class project Resources;
  • Create a resource file (go to Properties -> Resources of the project);
  • Move the resource file (default Resources.resx) to the root of the project;
  • Rename the resources file Resources.resx -> GlobalResources.resx;
  • Modify the resources file modifier to Public (double click on the resources file, "Access Modifier" -> "Public");
  • Copy any resource file to the root directory of the project;
  • Add any resource to the resource file using "Add Existing File" and selecting resources on the root directory of the project.

解决方案资源管理器中的项目应如下所示:

The project in the Solution Explorer should look like this:

现在在任何项目中,您都需要共享resx文件:

Now in any project you need the resx file to be shared do this:

  • 添加资源"项目作为参考;
  • 手动编辑项目文件(* .csproj),并在资源文件ItemGroup中添加以下行(如果要查看,请创建本地资源文件):

  • Add the "Resource" project as a dipendency;
  • Edit manually the project file (*.csproj) and add the following lines in the resources file ItemGroup (create a local resource file if you want to see it):

<ItemGroup>
  ...
  <EmbeddedResource Include="..\Resources\GlobalResources.resx">
    <Link>GlobalResources.resx</Link>
    <Generator>ResXFileCodeGenerator</Generator>
    <LastGenOutput>GlobalResources.Designer.cs</LastGenOutput>
    <CustomToolNamespace>Resources</CustomToolNamespace>
  </EmbeddedResource>
  ...
</ItemGroup>

显然,包含"属性应该是指向GlobalResources.resx的正确相对路径.

Obviously, the "Include" attribute should be the correct relative path to GlobalResources.resx.

  • 按照您的要求使用设计器中的资源.

在我的项目中,执行以下操作会自动生成以下行并将其添加到项目中:

In my project, the following lines are generated and added to the project automatically when I do this:

<Compile Include="..\Resources\GlobalResources.Designer.cs">
  <Link>GlobalResources.Designer.cs</Link>
  <AutoGen>True</AutoGen>
  <DesignTime>True</DesignTime>
  <DependentUpon>GlobalResources.resx</DependentUpon>
</Compile>

如果未添加,请手动添加.

If they aren't added, add them manually.

现在,该项目在解决方案资源管理器中应该看起来像这样(喜欢的文件应该有不同的标记):

Now the project should look like this in the Solution Explorer (likend files should be marked differently):

最后两个步骤:

  • 单击链接的"GlobalResources.resx",然后在属性"上将无"设置为"BuildAction". 自定义工具命名空间"应该已经设置为资源";

  • 单击链接的"GlobalResources.Designer.resx",然后在属性"中将无"设置为"BuildAction".

您已完成:至此,您应该真正能够使用设计器中共享resx中添加的资源,在问题的项目资源文件"对话框中选择"GlobalResources.resx".并且正如所告诉的,它甚至在运行时也确实可以共享!在属性"面板中,您应该看到外部项目的完整名称空间和类.如果删除了对资源"项目的依赖,它甚至不会编译.如果它对您不起作用,请坚持直到它起作用,以防万一告诉我该指南在哪里有问题.它必须工作!让我知道.

And you are done: at this point you should really be able to use the resources you added in the shared resx in the designer selecting "GlobalResources.resx" in the "Project Resource file" dialog of your question. And as told it's really shared even at runtime! In the Properties panel you should see the full Namespace and class of the external project. If you remove the dependency on the "Resource" project it doesn't even compile. If it doesn't work for you, insist until it works and in case tell me where the guide is wrong. It have to work! Let me know.

这篇关于在Visual Studio中跨项目共享WinForms资源的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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