确定ProgressBar显示使用Windows :: Web :: Http :: HttpClient下载文件的进度

编程入门 行业动态 更新时间:2024-10-28 00:22:06
本文介绍了确定ProgressBar显示使用Windows :: Web :: Http :: HttpClient下载文件的进度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想在我的应用程序(XAML + C ++)中显示一个确定的ProgressBar控件,显示下载文件的进度。现在我只实现了不确定的ProgressBar控件。我使用Windows :: Web :: Http :: HttpClient使用以下代码创建异步下载任务:

try { //执行异步GET。我们需要将use_current()与continuation一起使用,因为任务在 //后台线程上完成,我们需要在UI线程上运行以更新UI。 OperationProgress-> IsIndeterminate = true; OperationProgress->可见性= Windows :: UI :: Xaml ::可见性::可见; create_task(m_httpClient-> GetAsync(resourceAddress),m_cancellationTokenSource.get_token())。then([this,pltfmstrFilename](HttpResponseMessage ^ response) { //抛出如果HTTP响应的IsSuccessStatusCode属性为false,则为异常。 response-> EnsureSuccessStatusCode(); //将内容读入本地缓冲区返回create_task(响应 - > Content-> ReadAsBufferAsync(),m_cancellationTokenSource.get_token())。then( [this,pltfmstrFilename,response](IBuffer ^ contentBuffer) { //保存缓冲区内容到本地文件 if(response-> StatusCode == HttpStatusCode :: Ok) { StorageFolder ^ tempFolder; tempFolder = ApplicationData :: Current-> TemporaryFolder ; create_task(tempFolder-> CreateFileA sync(pltfmstrFilename,CreationCollisionOption :: ReplaceExisting))。then([this,contentBuffer](StorageFile ^ file) { if(file){ //保存下载的内部代码数据到文件 //为了清楚起见,这里省略了} },task_continuation_context :: use_current()); } },task_continuation_context :: use_current()); },task_continuation_context :: use_current())。then([=](task< void> previousTask) { try { OperationProgress-> Visibility = Windows :: UI :: Xaml :: Visibility :: Collapsed; //检查以前的任何任务是否引发异常。 previousTask.get(); NotifyUser(" Task completed" ;); } catch(const task_canceled&) { NotifyUser("任务取消。"); } catch(异常) ^ ex) { NotifyUser(" Error:" + ex-> Message); } },task_continuation_context :: use_current()); } catch(Exception ^ ex) { OperationProgress-> Visibility = Windows :: UI :: Xaml :: Visibility :: Collapsed; NotifyUser(" Error:" + ex-> Message); }

使用确定的ProgressBar控件可以提供更好的用户体验。有人可以帮忙吗?谢谢。

解决方案

HttpClient GetAsync方法有一个报告进度的重载。 也许这会有所帮助

https:// msdn .microsoft / EN-US /库/窗/应用/ dn298645

I want to show a determinate ProgressBar control in my app (XAML + C++) showing progress of downloading a file. For now I have only implemented the indeterminate ProgressBar control. I use Windows::Web::Http::HttpClient to create an asynchronous download task using the code below:

try { // Do an asynchronous GET. We need to use use_current() with the continuations since the tasks are completed on // background threads and we need to run on the UI thread to update the UI. OperationProgress->IsIndeterminate = true; OperationProgress->Visibility = Windows::UI::Xaml::Visibility::Visible; create_task(m_httpClient->GetAsync(resourceAddress), m_cancellationTokenSource.get_token()).then([this, pltfmstrFilename](HttpResponseMessage^ response) { // Throws an exception if the IsSuccessStatusCode property for the HTTP response is false. response->EnsureSuccessStatusCode(); // Read the content into a local buffer return create_task(response->Content->ReadAsBufferAsync(), m_cancellationTokenSource.get_token()).then( [this,pltfmstrFilename,response](IBuffer^ contentBuffer) { // Save the buffer content to a local file if (response->StatusCode == HttpStatusCode::Ok) { StorageFolder^ tempFolder; tempFolder = ApplicationData::Current->TemporaryFolder; create_task(tempFolder->CreateFileAsync(pltfmstrFilename, CreationCollisionOption::ReplaceExisting)).then([this, contentBuffer](StorageFile^ file) { if ( file ) { // in-house code to save downloaded data to a file // omitted here for clarity } }, task_continuation_context::use_current()); } }, task_continuation_context::use_current()); }, task_continuation_context::use_current()).then([=](task<void> previousTask) { try { OperationProgress->Visibility = Windows::UI::Xaml::Visibility::Collapsed; // Check if any previous task threw an exception. previousTask.get(); NotifyUser("Task completed"); } catch (const task_canceled&) { NotifyUser("Task canceled."); } catch (Exception^ ex) { NotifyUser("Error: " + ex->Message); } }, task_continuation_context::use_current()); } catch (Exception^ ex) { OperationProgress->Visibility = Windows::UI::Xaml::Visibility::Collapsed; NotifyUser("Error: " + ex->Message); }

It would be better to use a determinate ProgressBar control that offers better user experience. Could someone help? Thanks.

解决方案

The HttpClient GetAsync method has an overload that reports progress. Maybe that will help

msdn.microsoft/en-us/library/windows/apps/dn298645

更多推荐

确定ProgressBar显示使用Windows :: Web :: Http :: HttpClient下载文件的进度

本文发布于:2023-10-07 01:34:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1468066.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:进度   文件   Windows   ProgressBar   Web

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!