熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

在silverlight中使用downloader

2013-11-13 10:13:19  來源: .NET編程 

  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);
            }
        }


        好了今天的內容就到這裡了


From:http://tw.wingwit.com/Article/program/net/201311/12957.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.