ITPub博客

首页 > Linux操作系统 > Linux操作系统 > 在silverlight中使用downloader

在silverlight中使用downloader

原创 Linux操作系统 作者:iDotNetSpace 时间:2009-05-11 16:51:39 0 删除 编辑
      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代码如下所示:          
           下载图片
           下载xaml
        
        <!-- 下载进度条 -->
        
                      Fill="Maroon" />
                      StrokeThickness="1" Stroke="Black" />
                      Text="0%" FontSize="12" />
        
        
         

接下来就是相应的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);
            }
        }

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/12639172/viewspace-594746/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2008-01-04

  • 博文量
    2376
  • 访问量
    5321386