电脑故障

位置:IT落伍者 >> 电脑故障 >> 浏览文章

在silverlight中使用downloader


发布日期:2018/4/22
 

downloader对象为Silverlight 插件提供了下载功能其通过HTTP(get)请求异步下载指定的内容

downloader所支持的event包括

Completed下载完成 (Downloader)

DownloadFailed 下载失败

DownloadProgressChanged下载进度变化 (Downloader)

支持的方法包括

Abort Equals FindName (DependencyObject) GetHost GetResponseText GetValue Open

Send SetValue

下面这个DEMO演示使用downloader从网上下载jpg图片以及xaml文件并完成数据加载的过程

其xaml代码如下所示 <Canvas CanvasLeft=>

<TextBlock CanvasLeft= CanvasTop= Name=StartDownLoadImg>下载图片</TextBlock>

<TextBlock CanvasLeft= CanvasTop= Name=StartDownLoadXaml>下载xaml</TextBlock>

</Canvas>

<! 下载进度条 >

<Canvas CanvasTop=>

<Rectangle Name=progressRectangle CanvasLeft= Height= Width=

Fill=Maroon />

<Rectangle CanvasTop = CanvasLeft= Height= Width=

StrokeThickness= Stroke=Black />

<TextBlock Name=progressText CanvasTop = CanvasLeft=

Text=% FontSize= />

</Canvas>

<Image Name=loadimg Height= Width= />

<Canvas CanvasTop= x:Name=loadxaml/>

接下来就是相应的js代码(详情见注释) Silverlight_JsWebDownloader = function()

{

}

Silverlight_JsWebDownloaderprototype =

{

handleLoad: function(plugIn userContext sender)

{

thisplugIn = plugIn;

// 附加事件处理程序

senderfindName(StartDownLoadImg)addEventListener(MouseLeftButtonDown

SilverlightcreateDelegate(this thishandleMouseLeftButtonDown));

senderfindName(StartDownLoadXaml)addEventListener(MouseLeftButtonDown

SilverlightcreateDelegate(this thishandleMouseLeftButtonDown));

progressText = senderfindName(progressText);

progressRectangle = senderfindName(progressRectangle);

}

//开始下载

handleMouseLeftButtonDown:function (sender eventArgs)

{

var slPlugin = sendergetHost();

//创建downloader对象

var downloader = slPlugincreateObject(downloader);

//下载进度变化事件绑定

downloaderaddEventListener(downloadProgressChanged

SilverlightcreateDelegate(this thishandleDownloadProgressChanged));

//下载结束事件绑定

downloaderaddEventListener(completed

SilverlightcreateDelegate(this thishandleCompleted));

//下载失败事件绑定

downloaderaddEventListener(downloadFailed

SilverlightcreateDelegate(this thishandleDownloadFailed));

//初始化下载请求

if(sendername == StartDownLoadImg) {

downloaderopen(GET ClientBin/tangjpg);

}

else {

downloaderopen(GET xaml/PathScenexaml);

}

//downloaderopen(GET contosoTTF);字体下载

//执行下载请求

downloadersend();

}

//下载进度改变

handleDownloadProgressChanged:function (sender eventArgs)

{

var percentage = Mathfloor(senderdownloadProgress * );

progressTexttext = percentage + %;

progressRectanglewidth = percentage * ;

}

//下载完成

handleCompleted:function (sender eventArgs)

{

var plugin = sendergetHost();

if (senderuri == ClientBin/tangjpg)

{

//绑定图片信息

senderfindName(loadimg)setSource(sender);

//清除 loadxaml 下的内容

senderfindName(loadxaml)childrenclear();

//var pathCanvas = senderfindName(pathCanvas);

//if (pathCanvas != null)

//{

// senderfindName(loadxaml)childrenremove(pathCanvas);

//}

//senderfindName(loadxaml)childrenremoveAt();

}

if (senderuri == xaml/PathScenexaml)

{

// 获取下载内容

var xamlFragment = senderResponseText;

// 创建内容

var xamlPath = ntentcreateFromXaml(xamlFragment);

senderfindName(loadxaml)childrenadd(xamlPath);

// 清除图片绑定

senderfindName(loadimg)Source = ;

}

// 下面为下载字体并进行加载的测试

// var myTextBlock = senderfindName(myTextBlock);

//myTextBlocksetFontSource(sender);

// 设置字体 Contoso Font

//myTextBlockfontFamily = Contoso Font;

//myTextBlocktext = This is my new font!;

}

//下载失败处理事件

handleDownloadFailed:function (sender errorArgs)

{

var errorMsg = Silverlight Error: \n\n;

errorMsg += Error Type: + errorArgserrorType + \n;

errorMsg += Error Message: + errorArgserrorMessage + \n;

errorMsg += Error Code: + errorArgserrorCode + \n;

switch(errorArgserrorType)

{

case RuntimeError:

if (errorArgslineNumber != )

{

errorMsg += Line: + errorArgslineNumber + \n;

errorMsg += Position: + errorArgscharPosition + \n;

}

errorMsg += MethodName: + thodName + \n;

break;

case ParserError:

errorMsg += Xaml File: + errorArgsxamlFile + \n;

errorMsg += Xml Element: + errorArgsxmlElement + \n;

errorMsg += Xml Attribute: + errorArgsxmlAttribute + \n;

errorMsg += Line: + errorArgslineNumber + \n;

errorMsg += Position: + errorArgscharPosition + \n;

break;

default:

break;

}

alert(errorMsg);

}

}

好了今天的内容就到这里了

上一篇:关于“验证码的制作”的一些补充

下一篇:类型初始值设定项引发异常