日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

.net内嵌资源

發布時間:2023/12/20 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 .net内嵌资源 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

ASP.NET(1.0/1.1)給我們提供了一個開發WebControl的編程模型,于是我們擺脫了asp里面的include模式的復用方式。不過 1.0/1.1提供的Web控件開發模型對于處理沒有image、css等外部資源的組件還算比較得心應手,script雖然很多時候也是外部資源,但在開發控件的時候我們習慣把script使用Page.Register...Script()來嵌入模塊,因為緊湊的東西更便于我們復用,用一個dll就可以解決問題又何必要節外生枝呢。

???????? ASP.NET 2.0提供的Web Resources管理模型,很好的解決了image、css、script等外部資源的管理問題。現在只需要在solution explorer把資源文件的build action屬性設為Embedded Resource。然后在assemblyinfo.cs里添加一句:

[assembly: WebResource("WebCtrl.cutecat.jpg", "image/jpg")]

??????? 我們可以看msdn里有WebResource的參數說明:第一個是資源的名字,第二個是資源的mime-type名。
????其實這個語句放在任何cs文件里,保證放在最高級namespace外就行。

??????? 然后在程序中調用如下:

m_Image.ImageUrl = this.Page.GetWebResourceUrl(typeof(WebCustom), "WebCtrl.cutecat.jpg");

??????? GetWebResourceUrl的第一個參數是用戶定義的類型(這個是用來確定assembly用的),第二個參數是資源名。

??????? 上面的語句返回給browser的代碼是:

<img src="WebResource.axd?a=pWebCtrl&amp;r=WebCtrl.cutecat.jpg&amp;t=632390947985312500" style="border-width:0px;" />

??????? 其中的src就是GetWebesourceUrl執行后返回的,它有3個參數(這里的&被解析成了&amp;,不過IIS也認的),第一個參數a是就是通過typeof(WebCustom)來確定的assembly的名字,第二個參數r很明顯就是資源的名字了,第三個參數t是一個a所指的assembly的timestamp。這個t是為了讓資源的引用能享用browser緩存的優化,因為IE對相同的url有自己的cache機制。又因為這個r同時又是用戶assembly文件的timestamp,如果用戶更新了代碼,重新編譯后t也會變化,這樣也就保證了browser能獲得最新的資源更新。如果我們能確定嵌入資源是確實不用再更新的,我們可以在typeof()里寫一個bcl里的類型,比如typeof(string),那么他將只在freamwork升級后才會變動這個t。

??????? 當然這個WebResource.axd是不存在的,它只是IIS中的一個ISAPI影射。

??????? 使用示例代碼如下:

#region WebResource Demo

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;

[assembly: WebResource(
"WebCtrl.cutecat.jpg", "image/jpg")]

namespace WebCtrl
{
??????? [DefaultProperty(
"Text")]
??????? [ToolboxData(
"<{0}:WebCustom runat=server></{0}:WebCustom>")]
????
public class WebCustom : WebControl
??????? {
????????
private string text;
????????
private Image m_Image;

??????????? [Bindable(
true)]
??????????? [Category(
"Appearance")]
??????????? [DefaultValue(
"")]
????????
public string Text
??????????? {
????????????
get { return text; }
????????????
set { text = value; }
??????????? }

????????
protected override void CreateChildControls()
??????????? {
??????????????? m_Image
= new Image();
????????????
this.Controls.Add(m_Image);
??????????? }

????????
protected override void Render(HtmlTextWriter output)
??????????? {
??????????????? m_Image.ImageUrl
= this.Page.ClientScript.GetWebResourceUrl(typeof(WebCustom), "WebCtrl.cutecat.jpg");
????????????
this.RenderChildren(output);
??????????? }
??????? }
}
#endregion

JS

1.向項目中添加Jscript文件
//script_1.js-----
function doClick1()
{
?????? alert("OK1_wufeng");
}
//script_2.js-----
function doClick2()
{
?????? alert("OK2");
}

2.解決方案資源管理器中,右鍵查看script_1.js和script_2.js的屬性,把高級中的“生成操作”屬性設置成“嵌入的資源”。

3.向AssemblyInfo.cs文件中添加如下行:(注意域名wf.ClientScriptResourceLabel)
[assembly: System.Web.UI.WebResource("wf.ClientScriptResourceLabel.script_1.js", "application/x-javascript")]
[assembly: System.Web.UI.WebResource("wf.ClientScriptResourceLabel.script_2.js", "application/x-javascript")]

4.向項目中添加一個類, 實例:
using System;
using System.Drawing;
using System.Web.UI;
using System.Web;
using System.Globalization;

namespace wf.ClientScriptResourceLabel
{
?????? public class ClientScriptResourceLabel : System.Web.UI.WebControls.WebControl
?????? {
?????????? //調用腳本資源
?????????? protected override void OnPreRender(EventArgs e)
?????????? {
?????????????? if (this.Page != null)
?????????????? {
?????????????????? this.Page.ClientScript.RegisterClientScriptResource(typeof(ClientScriptResourceLabel), "wf.ClientScriptResourceLabel.script_1.js");
?????????????????? this.Page.ClientScript.RegisterClientScriptResource(typeof(ClientScriptResourceLabel), "wf.ClientScriptResourceLabel.script_2.js");
?????????????? }
?????????????? base.OnPreRender(e);
?????????? }


?????????? /// <summary>
?????????? /// 呈現控件的方法RenderContents
?????????? /// </summary>
?????????? protected override void RenderContents(HtmlTextWriter output)
?????????? {
?????????????? output.AddAttribute("id", "1");
?????????????? output.AddAttribute("type", "checkbox");
?????????????? output.AddAttribute("value", "測試1");
?????????????? output.AddAttribute("onclick", "javascript:doClick1();");
?????????????? output.RenderBeginTag(HtmlTextWriterTag.Input);
?????????????? output.RenderEndTag();

?????????????? output.AddAttribute("id", "2");
?????????????? output.AddAttribute("type", "checkbox");
?????????????? output.AddAttribute("value", "測試2");
?????????????? output.AddAttribute("onclick", "javascript:doClick2();");
?????????????? output.RenderBeginTag(HtmlTextWriterTag.Input);
?????????????? output.RenderEndTag();

?????????????? base.RenderContents(output);
?????????? }
?????? }
}

總結

以上是生活随笔為你收集整理的.net内嵌资源的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。