我有一个WPF应用程序,点击一个按钮,应用程序进入一个计算,可以花4-10秒。我想更新不透明的背景,并显示一个进度条,在操作期间。
为此,我使用以下代码:
this.Cursor = System.Windows.Input.Cursors.Wait;
// grey-out the main window
SolidColorBrush brush1 = new SolidColorBrush(Colors.Black);
brush1.Opacity = 0.65;
b1 = LogicalTreeHelper.FindLogicalNode(this, "border1") as Border;
b1.Opacity = 0.7;
b1.Background = brush1;
// long running computation happens here ....
// show a modal dialog to confirm results here
// restore background and opacity here.
当我运行代码时,背景和不透明度在模态对话框出现之前不会改变。在计算开始之前,我怎样才能让这些视觉变化立即发生?在Windows窗体中,每个控件上都有一个Update()方法,我记得这是必要的。什么是WPF模拟?
发布于 2010-02-26 18:10:50
使用DoEvents()代码,如下所示:
.aspx
我的实际代码:
private void GreyOverlay()
{
// make the overlay window visible - the effect is to grey out the display
if (_greyOverlay == null)
_greyOverlay = LogicalTreeHelper.FindLogicalNode(this, "overlay") as System.Windows.Shapes.Rectangle;
if (_greyOverlay != null)
{
_greyOverlay.Visibility = Visibility.Visible;
DoEvents();
}
}
private void DoEvents()
{
// Allow UI to Update...
DispatcherFrame f = new DispatcherFrame();
Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background,
new Action<object>((arg)=> {
DispatcherFrame fr = arg as DispatcherFrame;
fr.Continue= false;
}), f);
Dispatcher.PushFrame(f);
}
发布于 2010-02-26 15:14:42
如果您要在后台线程中进行长时间运行的计算呢?完成后,将结果发送回UI线程..。
老实说,我怀疑没有别的东西可以解决你的问题。也许嵌套抽运能做到这一点,但我真的很怀疑。
以防万一这个引用有帮助:使用调度程序构建更多响应性应用程序
https://stackoverflow.com/questions/2342468
复制相似问题