WPF 中代码的奇怪行为(渲染问题)

发布于 2024-08-18 13:43:48 字数 1177 浏览 2 评论 0原文

在 WPF 应用程序中,我在窗口的代码隐藏文件中具有以下按钮单击事件处理程序:

   private void AddNewRecBtn_Click(object sender, RoutedEventArgs e)
    {
            DimRec.Visibility = System.Windows.Visibility.Visible;
            PrBarLayer.Visibility = System.Windows.Visibility.Visible;
            try
            {
                WriteAndClose();
            }
            catch (Exception)
            {
                throw;
            }
    }

这些代码行:

        DimRec.Visibility = System.Windows.Visibility.Visible;
        PrBarLayer.Visibility = System.Windows.Visibility.Visible;    

只需使用 Opacity= 使矩形 DimRec 可见"0.5" Fill="Gray" 用于在显示 ProgressBar 和带有 ProgressBar 本身的 StackPanel PrBarLayer 期间使窗口变暗。 WriteAndClose(); 方法执行很多操作,包括 LINQ to SQL 查询和屏幕上的大量可视化,这需要几秒钟(当它进行时,屏幕上没有任何反应,一切似乎都冻结了)。

在这几秒钟的时间里,我想向用户展示一个变暗的窗口控件和一个动画进度条,但问题是这些代码使这些图层可见,实际上并没有出现。如果我从此处理程序中删除 WriteAndClose(); 方法,则一切正常 - ProgressBar 可见。

看来 WPF 不会立即渲染视觉控件,然后将资源转移到下一个方法 WriteAndClose,一切都会冻结,然后当所有操作都已完成并且进度条层的可见性为使崩溃。

解决办法是什么?我怎样才能让ProgressBar先显示然后才做剩下的繁重的事情?

I a WPF application I have the following event-handler of a button-click in a code-behind file of a window:

   private void AddNewRecBtn_Click(object sender, RoutedEventArgs e)
    {
            DimRec.Visibility = System.Windows.Visibility.Visible;
            PrBarLayer.Visibility = System.Windows.Visibility.Visible;
            try
            {
                WriteAndClose();
            }
            catch (Exception)
            {
                throw;
            }
    }

These lines of code:

        DimRec.Visibility = System.Windows.Visibility.Visible;
        PrBarLayer.Visibility = System.Windows.Visibility.Visible;    

just make visible a rectangle DimRec with Opacity="0.5" Fill="Gray" for dimming the window during showing ProgressBar and a StackPanel PrBarLayer with a ProgressBar itself.
WriteAndClose(); method performs lots of stuff, including LINQ to SQL query and heavy visualization on the screen, which takes several seconds (while it's going on, there is nothig happening on the screen, everything seems freezed).

During this period of several seconds I'd like to show user a dimmed window control and a progress bar animated, but the problem is that these layers, which code make visible, in fact don't appear. If I remove WriteAndClose(); method from this handler, everything is OK - ProgressBar gets visible.

It seems that WPF doesn't render visually controls at once and then resources are taken to the next method WriteAndClose, everything freezes and then screen get rendered when all is done already and Visibility of layer of progress bar is made Collapsed.

What could be the solution? How I could make ProgressBar show first and only then do remaining heavy stuff?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倦话 2024-08-25 13:43:48

您必须将 WriteAndClose 的执行移至不同的线程。

但是,您将只能使用 [Control.]Dispatcher.BeginInvoke 来操作 UI。

You will have to move the executing of WriteAndClose to a different thread.

However, then you will only be able to manipulate the UI using [Control.]Dispatcher.BeginInvoke.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文