Abs*_*ith 8 c# windows-runtime windows-10-mobile uwp
我正在创建一个Windows 10 UWP应用程序,其中涉及BackgroundDownloader,这只适用于桌面而不是手机.
码:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
在该行之后await d.StartAsync().AsTask(cancellationToken.Token,progressCallback);
,程序不继续.而且也没有错误.这不仅适用于手机在桌面上完美运行!我错过了什么?
BackgroundDownloader 和我认为 Windows UWP 中的所有后台任务都很难与它们一起工作。您必须首先在当前解决方案中创建一个新解决方案作为 Windows 运行时组件。之后,您必须通过 Package.AppxManifest 链接它。呃,别忘了把运行时组件作为你主项目的参考。如果你做了这些事情,希望它一定是有效的。但请确保您有一个 RuntimeComponent 并将其链接到您的项目
归档时间: |
|
查看次数: |
1185 次 |
最近记录: |