处理多个确定进度条

时间:2016-03-03 21:23:11

标签: c# win-universal-app

首先,我创建了一个完整文件的gist以供参考。

我想要的是上传多个具有独占确定进度条的文件(每个文件单独的进度条)

我已成功完成上传进度,并且能够获得每个文件的单独进度百分比,如第164行所示

 Debug.WriteLine(String.Format(CultureInfo.CurrentCulture, "Progress: {0}, Status: {1}", upload.Guid, currentProgress.Status));

我目前正在通过Grid Binding列出文件,如第117行所示

 FA.Add(new FilesAttached
 {
     Filename = fname,
     FileSize = fsize,
     Percent = upload.Progress.BytesSent.ToString()
 });
 await coreDispatcher.RunAsync(CoreDispatcherPriority.Normal,
      () =>
           {
              attachments_all.ItemsSource = FA;
           });

我遇到的问题是进度根本没有变化,而且我相信有些不妥之处的确切摘录是

private async Task HandleUploadAsync(UploadOperation upload, bool start, string fname, string fsize)
        {
            CoreDispatcher coreDispatcher = Window.Current.Dispatcher;
            try
            {

                percent.Add("0");
                Debug.WriteLine("Running: " + upload.Guid, "");
                FA.Add(new FilesAttached
                {
                    Filename = fname,
                    FileSize = fsize,
                    Percent = upload.Progress.BytesSent.ToString()
                });
                await coreDispatcher.RunAsync(CoreDispatcherPriority.Normal,
                            () =>
                            {
                                attachments_all.ItemsSource = FA;
                            });
                Progress<UploadOperation> progressCallback = new Progress<UploadOperation>(UploadProgress);
                if (start)
                {
                    // Start the upload and attach a progress handler.
                    await upload.StartAsync().AsTask(cts.Token, progressCallback);
                }
                else
                {
                    // The upload was already running when the application started, re-attach the progress handler.
                    await upload.AttachAsync().AsTask(cts.Token, progressCallback);
                }

                ResponseInformation response = upload.GetResponseInformation();

                Debug.WriteLine(String.Format(CultureInfo.CurrentCulture, "Completed: {0}, Status Code: {1}", upload.Guid,
                    response.StatusCode), "");
            }
            catch (TaskCanceledException)
            {
                Debug.WriteLine("Canceled: " + upload.Guid, "");
            }
            catch (Exception ex)
            {
                if (!IsExceptionHandled("Error", ex, upload))
                {
                    throw;
                }
            }
        }
        private void UploadProgress(UploadOperation upload)
        {
            // UploadOperation.Progress is updated in real-time while the operation is ongoing. Therefore,
            // we must make a local copy at the beginning of the progress handler, so that we can have a consistent
            // view of that ever-changing state throughout the handler's lifetime.
            BackgroundUploadProgress currentProgress = upload.Progress;

            Debug.WriteLine(String.Format(CultureInfo.CurrentCulture, "Progress: {0}, Status: {1}", upload.Guid,
                currentProgress.Status));
            FilesAttached newFA = new FilesAttached();
            newFA.RaisePropertyChanged("Percent");
            double percentSent = 100;
            if (currentProgress.TotalBytesToSend > 0)
            {
                percentSent = currentProgress.BytesSent * 100 / currentProgress.TotalBytesToSend;

            }

            Debug.WriteLine(String.Format(CultureInfo.CurrentCulture,
                " - Sent bytes: {0} of {1} ({2}%)", currentProgress.BytesSent,
                currentProgress.TotalBytesToSend, percentSent));

            if (currentProgress.HasRestarted)
            {
                Debug.WriteLine(" - Upload restarted");
            }

            if (currentProgress.HasResponseChanged)
            {
                // We've received new response headers from the server.
                Debug.WriteLine(" - Response updated; Header count: " + upload.GetResponseInformation().Headers.Count);

                // If you want to stream the response data this is a good time to start.
                // upload.GetResultStreamAt(0);
            }
        }

感谢任何帮助或指示。

1 个答案:

答案 0 :(得分:1)

我有一个非常相似的下载管理器。你必须改变一下逻辑。 我有一个类SingleDownload.cs和一个DownloadManager.cs。

在单一下载中: 我有实施INotifyPropertyChanged,我有TitleDownloadLinkProgress等属性。我在SingleDownload中管理下载,因此很容易从内部更改进度。我还有Start()Stop()Dispose()等方法。

在DownloadManager.cs(也是一个页面)中,我有一个ObservableList<SingleDownload> downloads多个绑定到ListView的进度条绑定到进度。它就像一个魅力。

所以创建一个类ex。 SingleUpload将使用其Progress管理一个上传并创建上传列表并将其绑定到您的attachments_all