예제 #1
0
 public static double DownloadProgress(string id)
 {
     if (BatchDownloadManager.IsDownloaded(id))
     {
         return(100.0);
     }
     if (!BatchDownloadManager._dict.ContainsKey(id))
     {
         return(0.0);
     }
     return(BatchDownloadManager._dict[id]._lastProgress);
 }
예제 #2
0
 private void DownloadByIndex(int ind)
 {
     if (ind >= this._downloadList.Count)
     {
         EventAggregator.Current.Publish(new DownloadSucceededEvent()
         {
             Id = this._id
         });
         this.StoreInSettings();
         this._isInProgress = false;
     }
     else
     {
         string remoteUri = this._downloadList[ind].RemoteUri;
         string localPath = this._downloadList[ind].LocalPath;
         if (this.FileExists(localPath))
         {
             EventAggregator.Current.Publish(new DownloadProgressedEvent()
             {
                 Id       = this._id,
                 Progress = ((double)(ind + 1) * 100.0 / (double)this._downloadList.Count)
             });
             this.DownloadByIndex(ind + 1);
         }
         else
         {
             HttpWebRequest request = WebRequest.CreateHttp(remoteUri);
             request.BeginGetResponse((AsyncCallback)(iAR =>
             {
                 try
                 {
                     using (Stream responseStream = request.EndGetResponse(iAR).GetResponseStream())
                     {
                         using (MemoryStream memoryStream = BatchDownloadManager.ReadFully(responseStream))
                         {
                             memoryStream.Position = 0L;
                             using (IsolatedStorageFile storeForApplication = IsolatedStorageFile.GetUserStoreForApplication())
                             {
                                 using (IsolatedStorageFileStream storageFileStream = storeForApplication.OpenFile(localPath, FileMode.OpenOrCreate, FileAccess.Write))
                                     BatchDownloadManager.CopyStream((Stream)memoryStream, (Stream)storageFileStream);
                             }
                             memoryStream.Position = 0L;
                             ImageCache.Current.TrySetImageForUri(localPath, (Stream)memoryStream);
                         }
                     }
                     this._lastProgress = (double)(ind + 1) * 100.0 / (double)this._downloadList.Count;
                     EventAggregator.Current.Publish(new DownloadProgressedEvent()
                     {
                         Id = this._id,
                         Progress = this._lastProgress
                     });
                     this.DownloadByIndex(ind + 1);
                 }
                 catch (Exception ex)
                 {
                     Logger.Instance.Error("Failed to download batch id = " + this._id, ex);
                     this._isInProgress = false;
                     EventAggregator.Current.Publish(new DownloadFailedEvent()
                     {
                         Id = this._id
                     });
                 }
             }), null);
         }
     }
 }