简体   繁体   中英

BackgroundDownloader is not working for windows 10 mobile UWP?

I'm creating a windows 10 UWP app, which involves BackgroundDownloader this works only in the desktop and not on the phone.

Code:

    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;
    }
}

After the line await d.StartAsync().AsTask(cancellationToken.Token,progressCallback); the program doesn't proceed. And there are no errors too. This is not working only on the phone works perfectly on the desktop! What am I missing?

BackgroundDownloader and I think all BackgroundTasks in windows UWP hard to work with them . You have to first Create a new solution in your current solution as a Windows Runtime Component . After that you have to link it via Package.AppxManifest . Uh , Do not forget to make the runtime component as a reference of your main project . if you do these things hopefully it must be work . but be sure you have an RuntimeComponent and you linked it in to your project

We were struggling with exactly the same issue, but on a Universal Windows 10 app - not Phone. The culprit in our case was Windows 10's battery saver mode. On a Windows 10 device, tap the battery icon. This should bring up the battery and screen brightness flyout. Disable battery mode.

The effect was the same for any app using the Background downloader, including the MSDN download sample app.

Again, this was not on mobile but it did consistently happen on our Windows 10 app. Hope it's similar to phone.

在我的情况下,通过使用USB电缆将手机连接到笔记本电脑,相同的代码在Debug(调试)模式下不起作用,但在Release(释放)模式下下载并安装该应用程序,则该代码有效。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM