Silverlight:Downloader的使用(event篇)
生活随笔
收集整理的這篇文章主要介紹了
Silverlight:Downloader的使用(event篇)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
(1)Downloader的使用
首先我們看什么是Downloader,就是一個為描述Silverlight plug-in下載功能的集合.Downloader能異步的通過HTTP GET Request下載內容.他是一個能幫助Silverlight下載內容的一個對象,這些下載內容包括(XMAL content,JavaScript content,ZIP packages,Media,images).當Silverlight plug-in安裝時候,并不能完全提供所有應用程序的內容,但在一經請求這些內容就會響應應用程序的需要.更重要的是,你能在呈現內容后它仍然能被使用,而且不需要刷新業面.Downloader對象提供了初始數據傳輸的功能,通過事件和狀態性質監控數據傳輸過程,并重新找到數據過程.
這個Downloader 對象的屬性和方法模仿XMLHttpRequest(XHR)的一系列的APIs.XHR提供 JavaScript和其他Web瀏覽器腳本語言來傳輸和操作通過HTTP來于web Server端的XML數據.
通常你不詳細指定Downloader 的URI;但你能指定URIs,并使用URI的配置是最初在安裝Silverlight plug-in所指定主機器的HTML頁面的地址.Downloader不能被用來跨域下載文件(內容不是來于最初始主機也面的地址).
下面給出一個事列顯示怎樣create, initialize, and execute 一個下載請求.為一個 被操作者掛上兩個事件是(Completed, DownloadProgressChanged),但被操作者沒有被指定,還有DownloadFailed也沒有被定義.所以你要自己寫一個.
//?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
??? 當下載請求完成時沒有下載內容時觸發.
參數:
| ? 參數名 | 描述?? | ?類型 |
| ? downloaderObject | ?一個Downloader對象. | Object |
| ? token | ?是一個函數的返回值,能隨意的保留如變量.如你想要調用RemoveEventLister去刪除這個被操作者,你將需要這個參數 | ? integer |
| ? eventhandlerFunction | ?在腳本中被定義的你的事件函數名,當使用AddEventListener pararmeter,就不需要提供函數名. | ?Object |
| ? sender | ? 識別調用事件的對象? | ?Object |
| ? eventArgs?? | ?參數始終為 null?? | ?Object |
| ? | ? | ? |
DownloadFailed 當下載請求完成時沒有下載內容時觸發.異步的調用Downloader的Send方法.主要由Status和StatusText的屬性來反應HTTP的狀態值來確定錯誤的下載請求.
你還能在腳本中添加一個事件名來添加操作者:
downloaderObject.AddEventListener("DownloadFailed",?"eventhandlerFunction")
這里就回返回一個token;刪除操作時這個token就不是絕對需要,在這里是添加操作.所以需要.
DownloadFailed當下載請求完成時沒有下載內容時觸發,你使用一個Completed事件去取得Downloader對象所響應的文本.當你正在下載發生突發的錯誤,你將還是能添加一個DownloadFailed事件到你的應用程序和為DownloadFailed事件寫入適當代碼,最終解決你的內容不存在的問題.
(3)DownloadProgressChanged
在求情下載期間被出發.
參數:
| ? 參數名 | 描述?? | ?類型 |
| ? downloaderObject | ?一個Downloader對象. | Object |
| ? token | ?是一個函數的返回值,能隨意的保留如變量.如你想要調用RemoveEventLister去刪除這個被操作者,你將需要這個參數 | ? integer |
| ? eventhandlerFunction | ?在腳本中被定義的你的事件函數名,當使用AddEventListener pararmeter,就不需要提供函數名. | ?Object |
| ? sender | ? 識別調用事件的對象? | ?Object |
| ? eventArgs?? | ?參數始終為 null?? | ?Object |
| ? | ? | ? |
DownloadProgressChanged 事件能被使用監控下載進程.DownloadProgressChanged是按照被下載內容總長度的0.05顯示,最高可達到1.0.當下載狀態改變時都將出發Completed 或DownloadFailed.
你還能在腳本中添加一個事件名來添加操作者:
downloaderObject.AddEventListener("DownloadProgressChanged",?"eventhandlerFunction")
語法這里還是就回返回一個token;刪除操作時這個token就不是絕對需要,在這里是添加操作.所以需要.
在事件數據中這個進程要數不能被轉載.取而代之的是使用Downloader對象的屬性觸發事件.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
在請求下載內容完成時觸發.
Completed在請求下載內容完成時觸發.異步的調用Downloader的Send方法.主要由Status和StatusText的性質來反應HTTP的狀態值來確定錯誤的下載請求.
你還能在腳本中添加一個事件名來添加操作者:
downloaderObject.AddEventListener("Completed",?"eventhandlerFunction")
語法這里還是就回返回一個token;刪除操作時這個token就不是絕對需要,在這里是添加操作.所以需要.
Completed在請求下載內容完成時觸發.并返回下載內容,你使用一個Completed事件去取得Downloader對象所響應的文本,在選擇使用ResponseText性質或GetResponseText方法時.依賴于下載方式是否以包的形式,如是包的形式你就要指定part.
事例是定義一個取得下載內容的Completed事件函數,是單個文件下載(沒有使用包)
//?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);
}
總結
以上是生活随笔為你收集整理的Silverlight:Downloader的使用(event篇)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 梦到打小狗崽是什么意思
- 下一篇: 为什么经常梦到小学同学