在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文件并完成数据加载的过程:

     在silverlight中使用downloader

     在silverlight中使用downloader
  
  
     其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);
    } 
}

  

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



本文转自 daizhenjun 51CTO博客,原文链接:http://blog.51cto.com/daizhj/87764,如需转载请自行联系原作者

上一篇:QT应用编程: 基于Qt设计的跨平台录音机功能


下一篇:Java learn 对象流 及其编码时所遇到的细节问题