用资源文件路径替换绝对文件路径 [英] Replacing Absolute file path by Resource file path
问题描述
我为现有软件 (Revit) 创建了一个加载项,我想在加载项的按钮中嵌入一个图像(我公司的徽标).
I created an add-in to an existing software (Revit) and I want to embed an image (my company's logo) in the add-in's button.
这是我当前的代码:
'Set the large image shown on button
Dim uriImage As New Uri("\\10.8.60.3\Shared\REVIT\FSElogo.png")
Dim largeImage As New BitmapImage(uriImage)
MainButton.LargeImage = largeImage
它运行良好,徽标显示正确,但是它需要计算机能够访问位于 \\10.8.60.3
的服务器.在家工作时,我们无法访问此服务器,因此 Revit 在启动时会引发错误.
It works pretty well and the logo is correctly displayed, however it requires the computer to have access to the server located at \\10.8.60.3
. When working from home, we do not have access to this server and Revit throws an error when starting because of it.
所以我尝试将 FSElogo.png
文件作为 Resource
添加到我的 VB.Net 项目中,然后尝试使用 My.Resources
访问图像,有效地消除了使用外部图像的需要.
So I tried adding the FSElogo.png
file to my VB.Net project as a Resource
and then tried to use My.Resources
to access the image, effectively removing the need for an external image to be used.
好吧,我无法让它工作.我尝试用
Well, I can't get it to work. I tried replacing the code above by
MainButton.LargeImage = CType(My.Resources.ResourceManager.GetObject("FSElogo.png"), Windows.Media.ImageSource)
但它不起作用.它不会抛出错误,但按钮上没有显示图像.
But it doesn't work. It doesn't throw an error, but no image is displayed on the button.
如果我不将我的 Object
转换为 ImageSource
,我会收到一个 implicit conversion from Object to Image
错误,并且我甚至不确定我的 ResourceManager
是否真的返回了对象 FSElogo.png
.
If I don't cast my Object
to an ImageSource
I get an implicit conversion from Object to Image
error, and I'm not even sure my ResourceManager
is even really returning the object FSElogo.png
.
我在这里做错了什么?
推荐答案
我正在使用 VS 提供的带有 .BackgroundImage
属性的按钮.请注意,文件的扩展名不包含在资源标识符中.如果这不起作用,您必须准确解释您是如何将资源添加到项目中的.
I am using the VS provided button with the .BackgroundImage
property. Notice that the extension of the file is not included in the resource identifier. If this doesn't work, you will have to explain exactly how you added the resource to your project.
MainButton.BackgroundImage = My.Resources.FSElogo
这篇关于用资源文件路径替换绝对文件路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!