避免在 .resx 文件中硬编码图标文件路径
我试图让我们的应用程序处理不同的图标集(使其可以换肤)。我在此处询问了通常的操作方法。当我尝试应用答案中的解决方案时,我将 .resx 文件中的所有硬编码图标路径替换为使用环境变量的路径。例如,我将...替换
<data name="btnDel" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\..\Icons\btnDel.png;System.Drawing.Bitmap, System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
为:
<data name="btnDel" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>$(IconsFolder)\btnDel.png;System.Drawing.Bitmap, System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
然后,作为初始测试,我定义了一个名为 IconsFolder
的系统级环境变量,应用更改,重新启动 Visual Studio 并尝试构建。但我收到此错误:
D:\SVN.DRA.WorkingCopy\UserControl\My Project\Resources.resx(123,5): error MSB3103: Invalid Resx file. Could not find a part of the path 'D:\SVN.DRA.WorkingCopy\UserControl\My Project\$(IconsFolder)\btnDel.png'. Line 123, position 5.
.resx 文件似乎不理解环境变量。那么我怎样才能避免对这些路径进行硬编码呢?
编辑:每个图标都可以被多个项目引用,因此用于配置路径的任何机制的环境变量都必须在整个解决方案范围内可用,并且我应该能够设置它来自 MSBuild 脚本内部。
编辑2:我的所有表单都是在 C# 或 VB.NET 项目中定义的
I'm trying to get our app to handle different icon sets (make it sort of skinnable). I asked about the usual way to do it here. When I tried to apply the solution from the answer, I replaced all hardcoded icon paths in .resx files with paths using an environment variable. For example, I replaced...
<data name="btnDel" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\..\Icons\btnDel.png;System.Drawing.Bitmap, System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
...with:
<data name="btnDel" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>$(IconsFolder)\btnDel.png;System.Drawing.Bitmap, System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
Then, as an initial test, I defined a System-level environment variable called IconsFolder
, applied changes, restarted Visual Studio and tried to build. But I got this error:
D:\SVN.DRA.WorkingCopy\UserControl\My Project\Resources.resx(123,5): error MSB3103: Invalid Resx file. Could not find a part of the path 'D:\SVN.DRA.WorkingCopy\UserControl\My Project\$(IconsFolder)\btnDel.png'. Line 123, position 5.
It seems like .resx files don't understand environment variables. How can I avoid hardcoding those paths, then?
EDIT: Each icon can be referenced by more than one project, so the environment variable of whatever mechanism is used to configure the paths must be available on a solution-wide basis, and I should be able to set it from inside an MSBuild script.
EDIT 2: All my forms are defined in C# or VB.NET projects
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
系统范围的环境变量可能不起作用。您可能需要尝试按照此处的说明进行操作:
http://msdn.microsoft.com /en-us/library/ms173406.aspx
System wide environment variables may not work. You might want to try following the instructions here:
http://msdn.microsoft.com/en-us/library/ms173406.aspx
我认为没有办法使用资源文件来完成。实现此目的的一种方法是将位图放到项目上,右键单击它并将构建操作更改为“嵌入式资源”。在文本编辑器中打开项目文件并将路径更改为环境变量。 (这就是我们在 VS 2008 中的做法,从那时起他们就变得更容易了)。然后在代码中你可以这样做......
I don't think there is a way to accomplish using the resource file. One way to achieve this is to drop your bitmap on your project, right click it and change build action to "Embedded Resource". Open up the project file in text editor and change the path to your environment variable. (this is how we do it with VS 2008, they have made it easier since then). Then in code you can do this....