在我成功完成上传操作后,我现在正在尝试获得一些进度 UI。现在这是我的代码,包括进度回调和该回调的处理程序。
public async Task<UploadOperation> BackgroundUploadVideoAsync(IStorageFile file, string gfyName)
{
BackgroundUploader uploader = new BackgroundUploader();
//… Create upload ...
var partOperation = await uploader.CreateUploadAsync(new Uri("https://filedrop.gfycat.com/"), contentParts, "form-data", Guid.NewGuid().ToString() );
var progress = new Progress<UploadOperation>(Shell.Current.HandleUpload);
try
{
await partOperation.StartAsync().AsTask(progress);
return partOperation;
//var response = await operation.StartAsync().AsTask();
}
catch(Exception e)
{
var status = BackgroundTransferError.GetStatus(e.HResult);
//await new MessageDialog($"An error occurred: {status}").ShowAsync();
}
return partOperation;
}
public async void HandleUpload(UploadOperation operation)
{
await Dispatcher.RunAsync( Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
{
var percent = operation.Progress.BytesSent / operation.Progress.TotalBytesToSend;
progressBar.Value = Math.Truncate((double)percent);
});
}
}
问题是当HandleUpload
被调用时,'bytessent' 和 'bytestosend' 立即相等,我的进度条设置为 100% 并在那里等到任务完成。我以前从未处理过进度回调,所以我可能做错了。