Xamarin-CrossDownloadManager - 等待下载文件

时间:2017-03-24 20:43:32

标签: xamarin xamarin.forms download-manager

我使用Xamarin-CrossDownloadManager(https://github.com/SimonSimCity/Xamarin-CrossDownloadManager),我需要等待下载文件。我有这段代码:

private static async Task<bool> FileDownloadedTest(string LinkToFile, string PathFile)
    {
        var downloadManager = CrossDownloadManager.Current;
        CrossDownloadManager.Current.PathNameForDownloadedFile = new System.Func<IDownloadFile, string>(file => {
            return PathFile;
        });
        {
            await DeleteFile(PathFile);
            var file = downloadManager.CreateDownloadFile(LinkToFile);
            await Task.Run(() => downloadManager.Start(file, true)); //why this not wait???
        }         
        bool FileExist = await IsFileExist(PathFile);
        return FileExist;    
    }

为什么不等待完成下载操作?怎么做?

在他们写的图书馆网站上,我可以观看IDownloadManager.Queue以在下载文件时获取信息。但是,我不知道如何在我的方法中使用它......你能帮助我吗?

PS:对不起我的英语,我还在学习它;)

3 个答案:

答案 0 :(得分:2)

使用该库,文件下载完成时没有发布回调或事件,但您可以进行简单的检查并等待一些循环。

await Task.Run(async () =>
{
    var downloadManager = CrossDownloadManager.Current;
    var file = downloadManager.CreateDownloadFile(someFileBasedUrl);
    downloadManager.Start(file);
    bool isDownloading = true;
    while (isDownloading)
    {
        await Task.Delay(10 * 1000);
        isDownloading = IsDownloading(file);
    }
});

IsDownloading方法:

bool IsDownloading(IDownloadFile file)
{
    if (file == null) return false;

    switch (file.Status)
    {
        case DownloadFileStatus.INITIALIZED:
        case DownloadFileStatus.PAUSED:
        case DownloadFileStatus.PENDING:
        case DownloadFileStatus.RUNNING:
            return true;

        case DownloadFileStatus.COMPLETED:
        case DownloadFileStatus.CANCELED:
        case DownloadFileStatus.FAILED:
            return false;
        default:
            throw new ArgumentOutOfRangeException();
    }
}

回复:https://github.com/SimonSimCity/Xamarin-CrossDownloadManager/blob/develop/Sample/DownloadExample/Downloader.cs#L46

答案 1 :(得分:1)

SushiHangover 上面的回答效果很好,我将它与 ACR 用户对话框的包 (https://github.com/aritchie/userdialogs) 结合起来,在等待下载完成的同时向用户显示一个很好的加载进度屏幕。这在 Android 上运行良好(我还无法测试 iOS)。

   ...
            var downloadManager = CrossDownloadManager.Current;
            var fileToDownload = downloadManager.CreateDownloadFile(args.Url);
            downloadManager.Start(fileToDownload, true);                
            args.Cancel = true;
            UserDialogs.Instance.Progress("Downloading").Show();
            bool isDownloading = true;
            while (isDownloading)
            {
                await Task.Delay(100);
                if (fileToDownload.TotalBytesExpected > 0)
                {
                    UserDialogs.Instance.Progress().PercentComplete = (int)(fileToDownload.TotalBytesWritten / fileToDownload.TotalBytesExpected * 100);
                    Console.WriteLine(("DOWNLOAD PROGRESS: " + fileToDownload.TotalBytesWritten / fileToDownload.TotalBytesExpected * 100).ToString() + "%");
                }
                
                isDownloading = IsDownloading(fileToDownload);
            }
             UserDialogs.Instance.Progress().Hide();

            //Below code opens the download location after download has completed.
            Intent intent = new Intent(DownloadManager.ActionViewDownloads);
            intent.AddFlags(ActivityFlags.NewTask);
            Android.App.Application.Context.StartActivity(intent);
            return;
        }
    }

}

bool IsDownloading(IDownloadFile file)
{
    if (file == null) return false;

    switch (file.Status)
    {
        case DownloadFileStatus.INITIALIZED:
        case DownloadFileStatus.PAUSED:
        case DownloadFileStatus.PENDING:
        case DownloadFileStatus.RUNNING:
            return true;

        case DownloadFileStatus.COMPLETED:
        case DownloadFileStatus.CANCELED:
        case DownloadFileStatus.FAILED:
            return false;
        default:
            throw new ArgumentOutOfRangeException();
    }
}

答案 2 :(得分:0)

我不知道为什么IDownloadFile.Status = COMPLETED无法正常工作,但我找到了解决问题:

await Task.Run(() =>
        {
            var downloadManager = CrossDownloadManager.Current;
            var file = downloadManager.CreateDownloadFile(LinkToFile);
            downloadManager.Start(file);
            while (file.Status == DownloadFileStatus.INITIALIZED)
            {
                while (file.TotalBytesExpected > file.TotalBytesWritten)
                {
                    Debug.WriteLine(file.Status);
                }
            }
        });         

有人知道为什么DownloadFileStatus.INITIALIZED工作,但是DownloadFileStatus.COMPLETED没有?