我正在創建一個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對其進行測試如果它有效。 –