首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >WPF:如何立即将更改应用于不透明/背景?(WPF模拟WinForms Control.Update()方法?)

WPF:如何立即将更改应用于不透明/背景?(WPF模拟WinForms Control.Update()方法?)
EN

Stack Overflow用户
提问于 2010-02-26 15:00:31
回答 2查看 6.9K关注 0票数 1

我有一个WPF应用程序,点击一个按钮,应用程序进入一个计算,可以花4-10秒。我想更新不透明的背景,并显示一个进度条,在操作期间。

为此,我使用以下代码:

代码语言:javascript
运行
复制
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模拟?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-02-26 18:10:50

使用DoEvents()代码,如下所示:

.aspx

我的实际代码:

代码语言:javascript
运行
复制
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);
}
票数 0
EN

Stack Overflow用户

发布于 2010-02-26 15:14:42

如果您要在后台线程中进行长时间运行的计算呢?完成后,将结果发送回UI线程..。

老实说,我怀疑没有别的东西可以解决你的问题。也许嵌套抽运能做到这一点,但我真的很怀疑。

以防万一这个引用有帮助:使用调度程序构建更多响应性应用程序

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2342468

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档