Silverlight:Downloader的使用(event篇)
生活随笔
收集整理的這篇文章主要介紹了
Silverlight:Downloader的使用(event篇)
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
(1)Downloader的使用
首先我們看什么是Downloader,就是一個(gè)為描述Silverlight plug-in下載功能的集合.Downloader能異步的通過(guò)HTTP GET Request下載內(nèi)容.他是一個(gè)能幫助Silverlight下載內(nèi)容的一個(gè)對(duì)象,這些下載內(nèi)容包括(XMAL content,JavaScript content,ZIP packages,Media,images).當(dāng)Silverlight plug-in安裝時(shí)候,并不能完全提供所有應(yīng)用程序的內(nèi)容,但在一經(jīng)請(qǐng)求這些內(nèi)容就會(huì)響應(yīng)應(yīng)用程序的需要.更重要的是,你能在呈現(xiàn)內(nèi)容后它仍然能被使用,而且不需要刷新業(yè)面.Downloader對(duì)象提供了初始數(shù)據(jù)傳輸?shù)墓δ?通過(guò)事件和狀態(tài)性質(zhì)監(jiān)控?cái)?shù)據(jù)傳輸過(guò)程,并重新找到數(shù)據(jù)過(guò)程.
這個(gè)Downloader 對(duì)象的屬性和方法模仿XMLHttpRequest(XHR)的一系列的APIs.XHR提供 JavaScript和其他Web瀏覽器腳本語(yǔ)言來(lái)傳輸和操作通過(guò)HTTP來(lái)于web Server端的XML數(shù)據(jù).
通常你不詳細(xì)指定Downloader 的URI;但你能指定URIs,并使用URI的配置是最初在安裝Silverlight plug-in所指定主機(jī)器的HTML頁(yè)面的地址.Downloader不能被用來(lái)跨域下載文件(內(nèi)容不是來(lái)于最初始主機(jī)也面的地址).
下面給出一個(gè)事列顯示怎樣create, initialize, and execute 一個(gè)下載請(qǐng)求.為一個(gè) 被操作者掛上兩個(gè)事件是(Completed, DownloadProgressChanged),但被操作者沒(méi)有被指定,還有DownloadFailed也沒(méi)有被定義.所以你要自己寫(xiě)一個(gè).
//?Event?handler?for?initializing?and?executing?a?download?request.
function?onMouseLeftButtonUp(sender,?eventArgs)
{
????//?Retrieve?a?reference?to?the?plug-in.
????var?slPlugin?=?sender.getHost();
????//?Create?a?Downloader?object.
????var?downloader?=?slPlugin.createObject("downloader");
????//?Add?DownloadProgressChanged?and?Completed?events.
????downloader.addEventListener("downloadProgressChanged",?onDownloadProgressChanged);
????downloader.addEventListener("completed",?onCompleted);
????//?Initialize?the?Downloader?request.
????//?NOTE:?downloader?APIs?disallow?file:\\?scheme
????//?you?must?run?this?sample?over?localhost:?or?off?a?server?or?the?following?call?will?fail
????downloader.open("GET",?"promo.png");
????//?Execute?the?Downloader?request.
????downloader.send();
}
(2):DownloadFailed
??? 當(dāng)下載請(qǐng)求完成時(shí)沒(méi)有下載內(nèi)容時(shí)觸發(fā).
參數(shù):
| ? 參數(shù)名 | 描述?? | ?類型 |
| ? downloaderObject | ?一個(gè)Downloader對(duì)象. | Object |
| ? token | ?是一個(gè)函數(shù)的返回值,能隨意的保留如變量.如你想要調(diào)用RemoveEventLister去刪除這個(gè)被操作者,你將需要這個(gè)參數(shù) | ? integer |
| ? eventhandlerFunction | ?在腳本中被定義的你的事件函數(shù)名,當(dāng)使用AddEventListener pararmeter,就不需要提供函數(shù)名. | ?Object |
| ? sender | ? 識(shí)別調(diào)用事件的對(duì)象? | ?Object |
| ? eventArgs?? | ?參數(shù)始終為 null?? | ?Object |
| ? | ? | ? |
DownloadFailed 當(dāng)下載請(qǐng)求完成時(shí)沒(méi)有下載內(nèi)容時(shí)觸發(fā).異步的調(diào)用Downloader的Send方法.主要由Status和StatusText的屬性來(lái)反應(yīng)HTTP的狀態(tài)值來(lái)確定錯(cuò)誤的下載請(qǐng)求.
你還能在腳本中添加一個(gè)事件名來(lái)添加操作者:
downloaderObject.AddEventListener("DownloadFailed",?"eventhandlerFunction")
這里就回返回一個(gè)token;刪除操作時(shí)這個(gè)token就不是絕對(duì)需要,在這里是添加操作.所以需要.
DownloadFailed當(dāng)下載請(qǐng)求完成時(shí)沒(méi)有下載內(nèi)容時(shí)觸發(fā),你使用一個(gè)Completed事件去取得Downloader對(duì)象所響應(yīng)的文本.當(dāng)你正在下載發(fā)生突發(fā)的錯(cuò)誤,你將還是能添加一個(gè)DownloadFailed事件到你的應(yīng)用程序和為DownloadFailed事件寫(xiě)入適當(dāng)代碼,最終解決你的內(nèi)容不存在的問(wèn)題.
(3)DownloadProgressChanged
在求情下載期間被出發(fā).
參數(shù):
| ? 參數(shù)名 | 描述?? | ?類型 |
| ? downloaderObject | ?一個(gè)Downloader對(duì)象. | Object |
| ? token | ?是一個(gè)函數(shù)的返回值,能隨意的保留如變量.如你想要調(diào)用RemoveEventLister去刪除這個(gè)被操作者,你將需要這個(gè)參數(shù) | ? integer |
| ? eventhandlerFunction | ?在腳本中被定義的你的事件函數(shù)名,當(dāng)使用AddEventListener pararmeter,就不需要提供函數(shù)名. | ?Object |
| ? sender | ? 識(shí)別調(diào)用事件的對(duì)象? | ?Object |
| ? eventArgs?? | ?參數(shù)始終為 null?? | ?Object |
| ? | ? | ? |
DownloadProgressChanged 事件能被使用監(jiān)控下載進(jìn)程.DownloadProgressChanged是按照被下載內(nèi)容總長(zhǎng)度的0.05顯示,最高可達(dá)到1.0.當(dāng)下載狀態(tài)改變時(shí)都將出發(fā)Completed 或DownloadFailed.
你還能在腳本中添加一個(gè)事件名來(lái)添加操作者:
downloaderObject.AddEventListener("DownloadProgressChanged",?"eventhandlerFunction")
語(yǔ)法這里還是就回返回一個(gè)token;刪除操作時(shí)這個(gè)token就不是絕對(duì)需要,在這里是添加操作.所以需要.
在事件數(shù)據(jù)中這個(gè)進(jìn)程要數(shù)不能被轉(zhuǎn)載.取而代之的是使用Downloader對(duì)象的屬性觸發(fā)事件.That object is always the sender of the event.
------------------------------------------------------------
//?Event?handler?for?updating?visual?progress?indicator
function?onDownloadProgressChanged(sender,?eventArgs)
{
????//?Calculate?the?downloaded?percentage.
????var?percentage?=?Math.floor(sender.downloadProgress?*?100);
????//?Update?the?Rectangle?and?TextBlock?objects?of?the?visual?progress?indicator.
????progressText.text?=?percentage?+?"%";
????progressRectangle.width?=?percentage?*?2;
}
(4)Completed
在請(qǐng)求下載內(nèi)容完成時(shí)觸發(fā).
Completed在請(qǐng)求下載內(nèi)容完成時(shí)觸發(fā).異步的調(diào)用Downloader的Send方法.主要由Status和StatusText的性質(zhì)來(lái)反應(yīng)HTTP的狀態(tài)值來(lái)確定錯(cuò)誤的下載請(qǐng)求.
你還能在腳本中添加一個(gè)事件名來(lái)添加操作者:
downloaderObject.AddEventListener("Completed",?"eventhandlerFunction")
語(yǔ)法這里還是就回返回一個(gè)token;刪除操作時(shí)這個(gè)token就不是絕對(duì)需要,在這里是添加操作.所以需要.
Completed在請(qǐng)求下載內(nèi)容完成時(shí)觸發(fā).并返回下載內(nèi)容,你使用一個(gè)Completed事件去取得Downloader對(duì)象所響應(yīng)的文本,在選擇使用ResponseText性質(zhì)或GetResponseText方法時(shí).依賴于下載方式是否以包的形式,如是包的形式你就要指定part.
事例是定義一個(gè)取得下載內(nèi)容的Completed事件函數(shù),是單個(gè)文件下載(沒(méi)有使用包)
//?Event?handler?for?the?Completed?event.
function?onCompleted(sender,?eventArgs)
{
????//?Retrieve?downloaded?XAML?content.
????var?xamlFragment?=?sender.ResponseText;
????//?Create?the?objects?from?the?XAML?content.
????var?plugin?=?sender.getHost();
????var?button?=?plugin.content.createFromXaml(xamlFragment);
????//?Add?downloaded?XAML?content?to?the?root?Canvas?of?the?plug-in.
????var?rootCanvas?=?sender.findName("rootCanvas");
????rootCanvas.children.add(button);
}
總結(jié)
以上是生活随笔為你收集整理的Silverlight:Downloader的使用(event篇)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 梦到打小狗崽是什么意思
- 下一篇: sql management studi