如何在Web服务器控件中嵌入静态图像资源?
我将创建一个代表树视图的 Web 服务器控件。所以我想使用 2 个图像作为 + 和 - 来展开/折叠。如何将其构建到控件中,以便在页面上呈现时可以用作图像源?
由于这将位于已编译的 Web 控件库中,因此我不想在 Web 应用程序中依赖外部图像。
编辑:
基于这个答案 by Andre Kraemer 我执行了以下操作:
在 AssemblyInfo.vb 中:
<Assembly: System.Web.UI.WebResource("MyCompany.MyWebControls.Resources.plus.gif", "image/gif")>
<Assembly: System.Web.UI.WebResource("MyCompany.MyWebControls.Resources.minus.gif", "image/gif")>
在我的 RenderContents
覆盖中:
Dim lPlusImage As New WebControls.Image()
Dim lMinusImage As New WebControls.Image()
lPlusImage.ImageUrl = Page.ClientScript.GetWebResourceUrl(Me.GetType(), "MyCompany.MyWebControls.Resources.plus.gif")
lMinusImage.ImageUrl = Page.ClientScript.GetWebResourceUrl(Me.GetType(), "MyCompany.MyWebControls.Resources.minus.gif")
lPlusImage.RenderControl(output)
lMinusImage.RenderControl(output)
我的程序集名称是 MyWebControls
。
我的根命名空间是MyCompany.MyWebControls
。
图像 plus.gif
和 minus.gif
位于名为 Resources
的文件夹中,并且图像具有 Build Action 设置为嵌入式资源。
它仍然不起作用。我没有收到任何错误。我已经尝试直接在浏览器中生成的图像网址,机器人没有任何反应,只是一个空白页面。
注意:
我尝试在资源名称中使用无效路径,结果完全相同,这让我想知道是否需要做一些特殊的事情来将实际资源映射到资源名称。仅当我在代码中使用的名称与 AssemblyInfo 中指定的名称不同时,我才会收到 404 Not Found 错误,它与指向实际资源的路径无关!
编辑:
我找到了解决方案!
我发现这是C#和VB之间的区别。请参阅我自己的答案 这个问题。
I am going to create a web server control representing a treeview. So I want to use 2 images for + and - for expand/collapse. How can I build this into the control in a way that can be used as image source when rendered on the page?
Since this will be in a compiled web controls library, I don't want to rely on external images in the web application.
Edit:
Based on this answer by Andre Kraemer I did the following:
In AssemblyInfo.vb:
<Assembly: System.Web.UI.WebResource("MyCompany.MyWebControls.Resources.plus.gif", "image/gif")>
<Assembly: System.Web.UI.WebResource("MyCompany.MyWebControls.Resources.minus.gif", "image/gif")>
In my RenderContents
override:
Dim lPlusImage As New WebControls.Image()
Dim lMinusImage As New WebControls.Image()
lPlusImage.ImageUrl = Page.ClientScript.GetWebResourceUrl(Me.GetType(), "MyCompany.MyWebControls.Resources.plus.gif")
lMinusImage.ImageUrl = Page.ClientScript.GetWebResourceUrl(Me.GetType(), "MyCompany.MyWebControls.Resources.minus.gif")
lPlusImage.RenderControl(output)
lMinusImage.RenderControl(output)
My Assembly name is MyWebControls
.
My Root Namespace is MyCompany.MyWebControls
.
The images plus.gif
and minus.gif
are located in a folder named Resources
, and the images have Build Action set to Embedded Resource.
It still does not work. I get no errors. I have tried the generated image url directly in the browser, bot nothing happens, just a blank page.
Note:
I tried to use an invalid path in the resource name, and the result was exactly the same, which made me wonder if I need to do something special to map the actual resource to the resource name. I got a 404 Not Found error only if I used different name in the code than what was specified in AssemblyInfo, it had nothing to do with path was pointing to an actual resource!
Edit:
I found the solution!
I found out that it is a difference between C# and VB. See my own answer to this question.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将这两个图像添加到树视图控件项目的名为 images 的子文件夹中。然后将属性网格中的构建操作从内容更改为嵌入资源。
除此之外,您还必须在 assemblyinfo.cs 文件中将这两个图像注册为程序集的嵌入式资源,如下所示:
由于这些图像现在将嵌入到您的控件程序集中,您可以使用 ClientScriptManager 访问它们,如下所示:
add the two images to a subfolder called images of your treeview control project. Then change their build action in the property grid from content to embedded resource.
In addition to that, you have to register those two images as embedded resources for your assembly in the assemblyinfo.cs file like this:
As those images will be embedded into your controls assembly now, you can access them using the ClientScriptManager like this:
好的,我在 本文。我认为安德烈·克雷默(Andre Kraemer)是公认的答案,因为他为我指明了正确的方向。他不可能知道C#和VB的名字结构有细微的差别!
以下是适用于 VB 的解决方案:
重要的区别是在 VB 中,文件路径不包含在名称中。
在我的示例中,项目中的根命名空间是
MyCompany.MyWebControls
,图像文件位于名为Resources
的子文件夹中。在 C# 中,子文件夹是资源名称结构的一部分,但在 VB 中不是。在 C# 中,这将是:
在 VB 中,我们必须忽略资源名称路径中的文件路径:
因此,当我们知道这一点时,我的完整示例将是:
在 AssemblyInfo.vb 中:
在我的
RenderContents
覆盖中:瞧,它有效了!
OK, I found the answer in this article. I give credit to Andre Kraemer as the accepted answer, because he pointed me in the right direction. He could not know that there is a slight difference in the name structure between C# and VB!
Here is the solution that works for VB :
The important difference is that in VB, the file path is not included in the name.
In my example, the root namespace in the project is
MyCompany.MyWebControls
and the image files is in a subfolder namedResources
. In C#, the subfolder is part of the resource name structure, but not in VB.In C#, this would be:
In VB, we must ignore the file path in the resource name path:
So when we know this, my full example will be:
In AssemblyInfo.vb:
In my
RenderContents
override:And voilá, it works!
您的形象->属性->构建动作 ->嵌入式资源。它将与您的控件或库一起编译。
Your image -> Properties -> Build Action -> Embedded Resource. It will be compiled with your control or library.