我正在创建一个Windows 10 UWP应用程序,其中涉及BackgroundDownloader这只适用于桌面,而不是在手机上。BackgroundDownloader不适用于Windows 10移动版UWP?
代码:
var dl = new BackgroundDownloader();
dl.CostPolicy = BackgroundTransferCostPolicy.Always;
file = await localSoundsFolder.CreateFileAsync(name, CreationCollisionOption.ReplaceExisting);
if (file != null)
{
var d = dl.CreateDownload(new Uri(uriToDownloadFrom,UriKind.RelativeOrAbsolute), file);
d.Priority = BackgroundTransferPriority.High;
var progressCallback = new Progress<DownloadOperation>(x => DownloadProgress(x, sc));
try
{
await d.StartAsync().AsTask(cancellationToken.Token,progressCallback);
//After this line it doesn't progress!
CancellationTokenSource token = Utility.cancellationList[sc];
if (token != null)
{
token.Cancel();
Utility.cancellationList.Remove(sc);
Debug.WriteLine("The sc has been removed from the download list");
}
}
catch
{
return;
}
}
private static void DownloadProgress(DownloadOperation download,SoundClass sc)
{
Debug.WriteLine("Callback");
var value = download.Progress.BytesReceived * 100/download.Progress.TotalBytesToReceive;
Debug.WriteLine("The bytesReceived is {0} and total bytes is {1}", download.Progress.BytesReceived.ToString(), download.Progress.TotalBytesToReceive.ToString());
new System.Threading.ManualResetEvent(false).WaitOne(10);
sc.downloadProgress = value;
if (download.Progress.Status == BackgroundTransferStatus.Completed || value >= 100)
{
Debug.WriteLine("DONE donwloading the file {0}", download.ResultFile.Name);
Debug.WriteLine("The file name happened to be to be added was " + download.ResultFile.Name);
string fileName = download.ResultFile.Name;
}
}
await d.StartAsync().AsTask(cancellationToken.Token,progressCallback);
程序不进行行之后。而且也没有错误。这不仅仅能够在电话上正常工作!我错过了什么?
这只是一个提示,但在应用程序启动之后,应该枚举所有现有的DownloadOperations并将它们重新连接到当前会话。 BackgroundDownloader不支持同一URI的同时下载,所以如果挂载某处的DownloadOperation可能会导致问题。请参阅https://msdn.microsoft.com/library/windows/apps/br207126 – Liero
感谢提示Liero,试过这个,但它仍然不工作。 – AbsoluteSith
我在没有'SoundClass'的Mobile Emulator 10.0.10240中测试了你的代码,因为我不知道它是什么,你的代码运行良好。您是在模拟器还是在真实设备中测试?这是来自Microsoft的[后台传输示例](https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/BackgroundTransfer),您可以使用您在代码中使用的URI对其进行测试如果它有效。 –