chiname

  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 :: 管理 ::

一、

  1. 在solution explorer把资源文件的Build Action属性设为[Embedded Resource]
  2. 然后将下面这行
    [assembly: WebResource("WebCtrl.cutecat.jpg""image/jpg")]
    添加到
    在assemblyinfo.cs中。其实这个语句放任何cs文件里,保证放在最高级namespace外就行
    WebCtrl.cutecat.jpg是资源名字,比如项目的Default namespace是“wuChang.WebControls”,
    而图片文件MessagePanel_info.gif放在/images目录中,那么完整资源名就是wuChang.WebControls.images.MessagePanel_info.gif
  3. 然后在程序中调用如下:
    Image1.ImageUrl =  this.Page.ClientScript.GetWebResourceUrl(this.GetType(), "wuChang.WebControls.images.MessagePanel_info.gif");


二、使用资源文件
1、添加一资源文件MessagePanel.resx,放在/images/文件夹中,MessagePanel.resxBuild Action属性默认就是[Embedded Resource],无需改动
2、添加一字符串资源,比如名为MessagePanelTemplate,内容自定
3、调用方法:

string rsPath = this.GetType().Namespace + ".images.MessagePanel";
                System.Resources.ResourceManager rm 
= new System.Resources.ResourceManager(rsPath, System.Reflection.Assembly.GetExecutingAssembly());
                _templateString 
= rm.GetString("MessagePanelTemplate");

其中ResourceManager的第一个参数为资源的base naem,格式为:namespace + 资源文件名。比如MessagePanel.resx,则取MessagePanel。如果是MessagePanel.zh-cn.resx,也是取MessagePanel。
rm.GetString()的参数则是资源名字

三、在<head></head>中加入stylesheet  link
在/images/中添加MessagePanel.css,Build Action属性设为[Embedded Resource]
在最外层namespace,添加[assembly: WebResource("wuChang.WebControls.images.MessagePanel.css", "text/css")]
调用方法:
strCssLink = string.Format("\n<link href=\"{0}\" rel=\"stylesheet\" type=\"text/css\" />\n", this.Page.ClientScript.GetWebResourceUrl(this.GetType(), "wuChang.WebControls.images.MessagePanel.css"));
string cssKey = "MessagePanelName";
   if (this.Page.Header.FindControl(cssKey) != null)
    return;

   Literal ltlCss = new Literal();
   
   ltlCss = new Literal();
   ltlCss.ID = cssKey;
   ltlCss.Text = strCssLink;

   this.Parent.Page.Header.Controls.Add(ltlCss);


花了二天时间,边学边做,做了一个类似.Text中的MessagesPanel控件
源码在这里下载
MessagesPanel

参考:
http://www.cnblogs.com/birdshome/archive/2004/12/19/79309.html


另外想请教一个问题:
比如将某个.Text文本文件设为Embedded Resource的话,那在如何读出此文件的内容。

Feedback

# re: asp.net 2.0 自定义控件中的使用嵌入资源的一些总结  回复   

2005-11-25 12:08 by 无常
补充第二步:
刚才发现一个更简便的方法
在添加资源时vs2005自动生成了一个和资源文件名相同的class
可以直接这样
_templateString = wuChang.WebControls.images.MessagePanel.MessagePanelTemplate;
posted on 2006-06-14 09:21  把我的欢乐带给你  阅读(119)  评论(0)    收藏  举报