Xamarin-CrossDownloadManager语言 - 等待下载文件



我使用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:对不起我的英语,我还在学习;)

使用该库,不会在文件下载完成时发布回调或事件,但您可以进行简单的检查并等待更多循环。

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

我不知道

为什么 IDownloadFile.Status = 已完成不起作用,但我发现了解决问题:

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.DONE 不工作?

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

相关内容

  • 没有找到相关文章

最新更新