自定义进度条指示器在耗时操作之前未显示 [英] Custom ProgressBar Indicator not showing up before time consuming action

查看:21
本文介绍了自定义进度条指示器在耗时操作之前未显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的 Windows Phone 8 项目中实现了自定义进度条指示器.如果我尝试用按钮切换指示器,它工作正常.但是当然我希望它在我执行耗时的操作(用许多项目填充列表)时显示出来.但是当它阻塞 UI 时,进度条指示器不会在操作之前显示,而是在操作之后显示.在对列表进行修改之前,我在指标本身和整个页面上尝试了 .UpdateLayout(),但没有任何效果.

I implemented the custom progressbar indicator in my Windows Phone 8 project. It works fine if I try to toggle the indicator with a button. But of course I want it to show up while I perform time consuming actions (filling a list with many items). But as it blocks the UI the progressbar indicator doesn't show up before the action but only afterwards. I tried .UpdateLayout() on the indicator itself and the whole page before performing modifications to the list but none of it worked.

customIndeterminateProgressBar.Visibility = System.Windows.Visibility.Visible;

// add ~100 list items

customIndeterminateProgressBar.Visibility = System.Windows.Visibility.Collapsed;

有没有其他方法可以做到这一点?

Is there any other way to do this?

推荐答案

您可以将耗时的工作转移到新任务上,并在最后添加一个继续以设置进度条可见性.我在这里使用 任务并行库 实现这一目标:

You could offload your time consuming work to a new task and add a continuation to set progress bar visibility at the end. Here i'm using the Task Parallel Library to achieve this:

customIndeterminateProgressBar.Visibility = System.Windows.Visibility.Visible;

Task.Run(() =>
{
   // Do CPU intensive work
}).ContinueWith(task =>
{
   customIndeterminateProgressBar.Visibility = System.Windows.Visibility.Collapsed;
}, TaskScheduler.FromCurrentSynchronizationContext());

这篇关于自定义进度条指示器在耗时操作之前未显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆