在silverlight中使用downloader

     downloader对象为Silverlight 插件提供了下载功能,其通过HTTP(get)请求异步下载指定的内容.
 
     其英文定义如下:
 
      Represents the set of download functionality for a Silverlight plug-in. The downloader
can asynchronously download content that can be obtained through an HTTP GET request.


     downloader所支持的event包括:
 
     Completed:下载完成 (Downloader)
 
     DownloadFailed: 下载失败
 
     DownloadProgressChanged:下载进度变化 (Downloader)
 
 
    支持的方法包括:
 
    Abort, Equals, FindName (DependencyObject), GetHost, GetResponseText, GetValue, Open,
    Send, SetValue

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

    

    
 
 
     其xaml代码如下所示:
 
< Canvas   Canvas.Left ="10" >
   
< TextBlock  Canvas.Left ="10"  Canvas.Top ="5"  Name ="StartDownLoadImg" > 下载图片 </ TextBlock >
   
< TextBlock  Canvas.Left ="150"  Canvas.Top ="5"  Name ="StartDownLoadXaml" > 下载xaml </ TextBlock >
</ Canvas >

<!--  下载进度条  -->
< Canvas  Canvas.Top ="40" >
  
< Rectangle  Name ="progressRectangle"  Canvas.Left ="20"   Height ="10"  Width ="0"
    Fill
="Maroon"   />
  
< Rectangle  Canvas.Top  ="-1"  Canvas.Left ="19"  Height ="12"   Width ="202"
    StrokeThickness
="1"  Stroke ="Black"   />
  
< TextBlock  Name ="progressText"   Canvas.Top  ="-4"  Canvas.Left ="230"
    Text
="0%"  FontSize ="12"   />
</ Canvas >

< Image  Name ="loadimg"  Height ="400"  Width ="300"   />

< Canvas  Canvas.Top ="70"  x:Name ="loadxaml" />

     接下来就是相应的js代码(详情见注释):
 
Silverlight_JsWeb.Downloader = function()
{
}

Silverlight_JsWeb.Downloader.prototype =
{
    handleLoad: function(plugIn, userContext, sender)
    {
        this.plugIn = plugIn;
       
        // 附加事件处理程序
        sender.findName("StartDownLoadImg").addEventListener("MouseLeftButtonDown",
                       Silverlight.createDelegate(this, this.handleMouseLeftButtonDown));
        sender.findName("StartDownLoadXaml").addEventListener("MouseLeftButtonDown",
                       Silverlight.createDelegate(this, this.handleMouseLeftButtonDown));

        progressText = sender.findName("progressText");
        progressRectangle = sender.findName("progressRectangle");
    },
   
    //开始下载
    handleMouseLeftButtonDown:function (sender, eventArgs)
    {   

        var slPlugin = sender.getHost();
        //创建downloader对象
        var downloader = slPlugin.createObject("downloader");   
        //下载进度变化事件绑定
        downloader.addEventListener("downloadProgressChanged",
                  Silverlight.createDelegate(this, this.handleDownloadProgressChanged));
        //下载结束事件绑定
        downloader.addEventListener("completed",
                  Silverlight.createDelegate(this, this.handleCompleted));
        //下载失败事件绑定
        downloader.addEventListener("downloadFailed",
                  Silverlight.createDelegate(this, this.handleDownloadFailed));
       
        //初始化下载请求
        if(sender.name == 'StartDownLoadImg') {
            downloader.open("GET", "ClientBin/tang.jpg");
        }
        else {
            downloader.open("GET", "xaml/PathScene.xaml");           
        }      
        //downloader.open("GET", "contoso.TTF");字体下载
       
        //执行下载请求
        downloader.send();
      
    },
   
    //下载进度改变
    handleDownloadProgressChanged:function (sender, eventArgs)
    {   
        var percentage = Math.floor(sender.downloadProgress * 100);   
        progressText.text = percentage + "%";
        progressRectangle.width = percentage * 2;
    },
   
    //下载完成
    handleCompleted:function (sender, eventArgs)
    {   
        var plugin = sender.getHost();

        if (sender.uri == "ClientBin/tang.jpg")
        {
            //绑定图片信息
            sender.findName("loadimg").setSource(sender,"");
           
            //清除 loadxaml 下的内容 
            sender.findName("loadxaml").children.clear();
           
            //var pathCanvas = sender.findName("pathCanvas");
            //if (pathCanvas != null)
            //{               
            //    sender.findName("loadxaml").children.remove(pathCanvas);
            //}
            //sender.findName("loadxaml").children.removeAt(0);
        }
       
        if (sender.uri == "xaml/PathScene.xaml")
        {
            // 获取下载内容.
            var xamlFragment = sender.ResponseText;
            // 创建内容.
            var xamlPath = plugin.content.createFromXaml(xamlFragment);           
            sender.findName("loadxaml").children.add(xamlPath);
           
            // 清除图片绑定              
            sender.findName("loadimg").Source = "";
        }
      
      
         // 下面为下载字体并进行加载的测试.
         // var myTextBlock = sender.findName("myTextBlock");
         //myTextBlock.setFontSource(sender);
         // 设置字体 "Contoso Font".
         //myTextBlock.fontFamily = "Contoso Font";
         //myTextBlock.text = "This is my new font!";
    },
   
    //下载失败处理事件
    handleDownloadFailed:function (sender, errorArgs)
    {
        var errorMsg = "Silverlight Error: \n\n";

        errorMsg += "Error Type:    " + errorArgs.errorType + "\n";
        errorMsg += "Error Message: " + errorArgs.errorMessage + "\n";
        errorMsg += "Error Code:    " + errorArgs.errorCode + "\n";   

        switch(errorArgs.errorType)
        {
            case "RuntimeError":          
                if (errorArgs.lineNumber != 0)
                {
                    errorMsg += "Line: " + errorArgs.lineNumber + "\n";
                    errorMsg += "Position: " +  errorArgs.charPosition + "\n";
                }
                errorMsg += "MethodName: " + errorArgs.methodName + "\n";
                break;
            case "ParserError":           
                errorMsg += "Xaml File:      " + errorArgs.xamlFile      + "\n";
                errorMsg += "Xml Element:    " + errorArgs.xmlElement    + "\n";
                errorMsg += "Xml Attribute:  " + errorArgs.xmlAttribute  + "\n";
                errorMsg += "Line:           " + errorArgs.lineNumber    + "\n";
                errorMsg += "Position:       " + errorArgs.charPosition  + "\n";
                break;
            default:
                break;
        }
        alert(errorMsg);
    } 
}

 
    好了,今天的内容就到这里了。
   
    下载源码,请点击这里:)

本文出自 “代震军:http://t.sina..” 博客,转载请与作者联系!

你可能感兴趣的:(js,职场,silverlight,休闲,downloader)