本文介绍了在 WPF 中渲染 UIElement 期间等待屏幕的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用 PRISM 的 WPF 应用程序.我有一个登录屏幕,成功登录后会出现一个包含许多项目的 TileListView 的新视图.这需要超过 10 秒的时间来渲染,因为控制必须计算很多等等.所有这些都按照标准行为使用 UI 线程,因为 WPF 中的渲染是在 UI 线程中完成的.是否可以在单独的窗口或类似的东西中显示像微调器这样的 WaitControl 或只是一个简单的动画?现在到动画停止的时候,控件在 UI 线程中呈现.

I have a WPF application using PRISM. I have a login screen and on successfuly logging in a new view containing a TileListView with many items appears. This needs over 10 seconds to render because control has to calculate a lot etc. All this uses the UI thread by standard behaviour, because rendering in WPF is done in UI thread. Is it possible to display a WaitControl like a spinner or just a simple animation in seperate window or something like this? Now to animation stops at the time, the control is rendered in UI Thread.

推荐答案

您可以创建一个在单独线程中启动的新窗口.有关如何执行此操作的示例,请参阅以下博客文章.

You could create a new window that launches in a separate thread. Please refer to the following blog post for an example of how to do this.

在单独的线程中启动 WPF 窗口: http://reedcopsey.com/2011/11/28/launching-a-wpf-window-in-a-separate-thread-part-1/

然后,您只需在验证凭据后和繁重渲染即将开始之前启动显示窗口的线程.

Then you just start this thread that displays the window just after you have validated the credentials and just before your heavy rendering is about to begin.

这可能是你能做的最好的事情,它也应该很容易实现.

This is probably the best thing you can do and it should also be a pretty easy thing to implement.

编辑 - 包括上面链接中的代码以供后代记录:

Edit - Including code from above link to be recorded for posterity:

    using System.Threading;
    using System.Windows.Threading;

    void LoadWindowInThread()
    {
        Thread newWindowThread = new Thread(new ThreadStart(() =>
        {
            // Create our context, and install it:
            SynchronizationContext.SetSynchronizationContext(
                new DispatcherSynchronizationContext(
                    Dispatcher.CurrentDispatcher));

            // Create and configure the window
            Window1 tempWindow = new Window1();

            // When the window closes, shut down the dispatcher
            tempWindow.Closed += (s, e) =>
               Dispatcher.CurrentDispatcher.BeginInvokeShutdown(DispatcherPriority.Background);

            tempWindow.Show();
            // Start the Dispatcher Processing
            Dispatcher.Run();
        }));
        newWindowThread.SetApartmentState(ApartmentState.STA);
        // Make the thread a background thread
        newWindowThread.IsBackground = true;
        // Start the thread
        newWindowThread.Start();
    }

这篇关于在 WPF 中渲染 UIElement 期间等待屏幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 08:42