确定ProgressBar显示使用Windows :: Web :: Http :: HttpClient下载文件的进度 [英] Determinate ProgressBar showing progress of downloading a file with 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方法有一个报告进度的重载。 也许这会有所帮助
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
https://msdn.microsoft.com/en-us/library/windows/apps/dn298645
这篇关于确定ProgressBar显示使用Windows :: Web :: Http :: HttpClient下载文件的进度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!