silverlight如何使用 Downloader 物件非同步下載各式資源
if (!window.demoVideo21)
window.demoVideo21 = {};
demoVideo21.Scene = function()
{
}
demoVideo21.Scene.prototype =
{
handleLoad: function(plugIn, userContext, rootElement)
{
this.plugIn = plugIn;
// Sample button event hookup: Find the button and then attach event handlers
this.button = rootElement.children.getItem(0);
this.button.addEventListener("MouseEnter", Silverlight.createDelegate(this, this.handleMouseEnter));
this.button.addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.handleMouseDown));
this.button.addEventListener("MouseLeftButtonUp", Silverlight.createDelegate(this, this.handleMouseUp));
this.button.addEventListener("MouseLeave", Silverlight.createDelegate(this, this.handleMouseLeave));
},
// Sample event handlers
handleMouseEnter: function(sender, eventArgs)
{
// The following code shows how to find an element by name and call a method on it.
var mouseEnterAnimation = sender.findName("mouseEnter");
mouseEnterAnimation.begin();
},
handleMouseDown: function(sender, eventArgs)
{
var mouseDownAnimation = sender.findName("mouseDown");
mouseDownAnimation.begin();
},
handleMouseUp: function(sender, eventArgs)
{
var mouseUpAnimation = sender.findName("mouseUp");
mouseUpAnimation.begin();
// Put clicked logic here
var plugIn=sender.getHost();
var dl=plugIn.createObject("Downloader");
dl.addEventListener("Completed",onCompleted);
dl.addEventListener("DownloadProgressChanged",onDownloadProgressChanged);
dl.addEventListener("DownloadFailed",onDownloadFailed);
//下载请求
dl.open("get","test.zip");
//开始下载
dl.send();
},
handleMouseLeave: function(sender, eventArgs)
{
var mouseLeaveAnimation = sender.findName("mouseLeave");
mouseLeaveAnimation.begin();
}
}
function onCompleted(sender, EvengArgs)
{
var music=sender.findName("Music");
music.setSource(sender, "summer.Wma");
var movie=sender.findName("Movie");
movie.setSource(sender, "SampleVideo.wmv");
var img=sender.findName("Image");
img.setSource(sender, "downloader.jpg");
var strJs=sender.GetResponseText("js.js");
eval(strJs);
var xaml=sender.GetResponseText("asdf.xaml");
var obj=sender.getHost().content.createFromXaml(xaml,true);
var rootCanvas=sender.findName("rootCanvas");
rootCanvas.children.add(obj);
}
function onDownloadProgressChanged(sender, EvengArgs)
{
var txtProgress=sender.findName("txtProgress");
var p=Math.floor(sender.downloadProgress*100);
txtProgress.text=p+" %";
}
function onDownloadFailed(sender, EvengArgs)
{
alert("falied"+"\n code"+sender.status+"\n text:"+sender.statusText);
}
window.demoVideo21 = {};
demoVideo21.Scene = function()
{
}
demoVideo21.Scene.prototype =
{
handleLoad: function(plugIn, userContext, rootElement)
{
this.plugIn = plugIn;
// Sample button event hookup: Find the button and then attach event handlers
this.button = rootElement.children.getItem(0);
this.button.addEventListener("MouseEnter", Silverlight.createDelegate(this, this.handleMouseEnter));
this.button.addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.handleMouseDown));
this.button.addEventListener("MouseLeftButtonUp", Silverlight.createDelegate(this, this.handleMouseUp));
this.button.addEventListener("MouseLeave", Silverlight.createDelegate(this, this.handleMouseLeave));
},
// Sample event handlers
handleMouseEnter: function(sender, eventArgs)
{
// The following code shows how to find an element by name and call a method on it.
var mouseEnterAnimation = sender.findName("mouseEnter");
mouseEnterAnimation.begin();
},
handleMouseDown: function(sender, eventArgs)
{
var mouseDownAnimation = sender.findName("mouseDown");
mouseDownAnimation.begin();
},
handleMouseUp: function(sender, eventArgs)
{
var mouseUpAnimation = sender.findName("mouseUp");
mouseUpAnimation.begin();
// Put clicked logic here
var plugIn=sender.getHost();
var dl=plugIn.createObject("Downloader");
dl.addEventListener("Completed",onCompleted);
dl.addEventListener("DownloadProgressChanged",onDownloadProgressChanged);
dl.addEventListener("DownloadFailed",onDownloadFailed);
//下载请求
dl.open("get","test.zip");
//开始下载
dl.send();
},
handleMouseLeave: function(sender, eventArgs)
{
var mouseLeaveAnimation = sender.findName("mouseLeave");
mouseLeaveAnimation.begin();
}
}
function onCompleted(sender, EvengArgs)
{
var music=sender.findName("Music");
music.setSource(sender, "summer.Wma");
var movie=sender.findName("Movie");
movie.setSource(sender, "SampleVideo.wmv");
var img=sender.findName("Image");
img.setSource(sender, "downloader.jpg");
var strJs=sender.GetResponseText("js.js");
eval(strJs);
var xaml=sender.GetResponseText("asdf.xaml");
var obj=sender.getHost().content.createFromXaml(xaml,true);
var rootCanvas=sender.findName("rootCanvas");
rootCanvas.children.add(obj);
}
function onDownloadProgressChanged(sender, EvengArgs)
{
var txtProgress=sender.findName("txtProgress");
var p=Math.floor(sender.downloadProgress*100);
txtProgress.text=p+" %";
}
function onDownloadFailed(sender, EvengArgs)
{
alert("falied"+"\n code"+sender.status+"\n text:"+sender.statusText);
}