WPF 启动屏幕直到 Windows 结束加载

发布于 2024-11-09 17:53:59 字数 2497 浏览 1 评论 0原文

我有一个应用程序在窗口加载时消耗大量时间。 在 Window_load 事件中,我从数据库读取一些控件的状态和名称。 我想做一个启动画面,该启动画面将在窗口完全加载后结束。

我尝试过这个例子 http://www.codeproject.com/KB/dialog/wpf_animated_text_splash .aspx 但启动屏幕在主窗口完全加载之前关闭,并且我的主窗口显示为白色并且未完全加载。

我是 wpf 的初学者,我不知道如何才能让启动屏幕保留在屏幕上,直到主窗口完全加载。

请给我一个例子。

我的启动屏幕代码:

public partial class SplashWindow : Window
    {
        Thread loadingThread;
        Storyboard Showboard;
        Storyboard Hideboard;
        private delegate void ShowDelegate(string txt);
        private delegate void HideDelegate();
        ShowDelegate showDelegate;
        HideDelegate hideDelegate;

        public SplashWindow()
        {
            InitializeComponent();
            showDelegate = new ShowDelegate(this.showText);
            hideDelegate = new HideDelegate(this.hideText);
            Showboard = this.Resources["showStoryBoard"] as Storyboard;
            Hideboard = this.Resources["HideStoryBoard"] as Storyboard;

        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            loadingThread = new Thread(load);
            loadingThread.Start();
        }
        private void load()
        {
            Thread.Sleep(6000);

            this.Dispatcher.Invoke(showDelegate, "first data to loading");
            Thread.Sleep(6000);
            //load data 
            this.Dispatcher.Invoke(hideDelegate);

            Thread.Sleep(6000);
            this.Dispatcher.Invoke(showDelegate, "second data loading");
            Thread.Sleep(6000);
            //load data
            this.Dispatcher.Invoke(hideDelegate);


            Thread.Sleep(6000);
            this.Dispatcher.Invoke(showDelegate, "last data loading");
            Thread.Sleep(6000);
            //load data 
            this.Dispatcher.Invoke(hideDelegate);



            //close the window
            Thread.Sleep(6000);
            this.Dispatcher.Invoke(DispatcherPriority.Normal,(Action)delegate() { Close(); });
        }
        private void showText(string txt)
        {
            txtLoading.Text = txt;
            BeginStoryboard(Showboard);
        }
        private void hideText()
        {
            BeginStoryboard(Hideboard);
        }

    }

我将在 MainWindow 构造函数中调用此启动屏幕:

new SplashWindow().ShowDialog();

但是我的 MainWindow Load 函数将在启动窗口完成显示后运行。

谢谢你!

I have an application that consumes a lot of time when the window loading.
In the Window_load event, I read from the database the state and the name of some controls.
I want to do a splash screen that will ends after the window will fully load.

I have tried with this example http://www.codeproject.com/KB/dialog/wpf_animated_text_splash.aspx but the splash screen closes before the main window is fully loaded and my mainwindow appears in white and is not fully loaded.

I am beginner in wpf, and I don't know how can I have a splash screen which remain on the screen until the main window fully loads.

Please give me an example.

My Splash Screen Code:

public partial class SplashWindow : Window
    {
        Thread loadingThread;
        Storyboard Showboard;
        Storyboard Hideboard;
        private delegate void ShowDelegate(string txt);
        private delegate void HideDelegate();
        ShowDelegate showDelegate;
        HideDelegate hideDelegate;

        public SplashWindow()
        {
            InitializeComponent();
            showDelegate = new ShowDelegate(this.showText);
            hideDelegate = new HideDelegate(this.hideText);
            Showboard = this.Resources["showStoryBoard"] as Storyboard;
            Hideboard = this.Resources["HideStoryBoard"] as Storyboard;

        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            loadingThread = new Thread(load);
            loadingThread.Start();
        }
        private void load()
        {
            Thread.Sleep(6000);

            this.Dispatcher.Invoke(showDelegate, "first data to loading");
            Thread.Sleep(6000);
            //load data 
            this.Dispatcher.Invoke(hideDelegate);

            Thread.Sleep(6000);
            this.Dispatcher.Invoke(showDelegate, "second data loading");
            Thread.Sleep(6000);
            //load data
            this.Dispatcher.Invoke(hideDelegate);


            Thread.Sleep(6000);
            this.Dispatcher.Invoke(showDelegate, "last data loading");
            Thread.Sleep(6000);
            //load data 
            this.Dispatcher.Invoke(hideDelegate);



            //close the window
            Thread.Sleep(6000);
            this.Dispatcher.Invoke(DispatcherPriority.Normal,(Action)delegate() { Close(); });
        }
        private void showText(string txt)
        {
            txtLoading.Text = txt;
            BeginStoryboard(Showboard);
        }
        private void hideText()
        {
            BeginStoryboard(Hideboard);
        }

    }

And this splash screen I will call in my MainWindow constructor:

new SplashWindow().ShowDialog();

But my MainWindow Load function will run after the Splash Window will finish to be showed.

Thank you!

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

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

发布评论

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

评论(4

数理化全能战士 2024-11-16 17:53:59

如果您使用内置的 SplashScreen 类,您可以调用 Show(false) 指定您将负责关闭启动屏幕。然后,您可以使用 Close()方法来关闭它。

请注意,SplashScreen 类仅支持显示静态图像。不过,这样做有很好的理由——尽快将启动屏幕显示在用户面前。

代码看起来像这样:

static class Entry
{
    static void Main(string[] args)
    {
        var splashScreen = new SplashScreen("path/to/your/image.png");
        splashScreen.Show(false);

        InitializeLogging();
        InitializeServices();
        InitializeUserInterface();
        InitializeWhateverElseYouNeed();

        splashScreen.Close(TimeSpan.FromSeconds(1));
    }
}

If you use the built-in SplashScreen class, you can call Show(false) to specify that you will be responsible for closing the splash screen. You can then use the Close() method to close it.

Note that the SplashScreen class only supports displaying a static image. It does this for very good reasons though - to get the splash screen up in front of your user as soon as possible.

Code would look something like this:

static class Entry
{
    static void Main(string[] args)
    {
        var splashScreen = new SplashScreen("path/to/your/image.png");
        splashScreen.Show(false);

        InitializeLogging();
        InitializeServices();
        InitializeUserInterface();
        InitializeWhateverElseYouNeed();

        splashScreen.Close(TimeSpan.FromSeconds(1));
    }
}
椵侞 2024-11-16 17:53:59

动态启动画面(带有进度更新等)

为了获得最佳结果,您可以对 SplashScreen 使用两阶段方法:

第 1 阶段。甚至在 .NET 代码开始加载之前就显示静态启动画面。

静态图像加载在 .NET 初始化之前使用本机代码。非常有效地尽快通知用户。有一种一种特殊方法实现这一点。

第 2 阶段。加载 .NET 后显示自定义表单。

设计您的自定义表单,使其与最初显示时的静态初始屏幕相同。显示表单将自动淡出第 1 阶段中显示的静态启动屏幕(由 WPF 完成),从那时起您就可以自由显示应用程序的加载进度。当主窗口加载完数据后,隐藏此表单(可以是总在顶部窗口)。

Dynamic splash screen (with progress updates etc.)

For the best results, you can use two-phase approach to SplashScreen:

Phase 1. Display static splash screen even before .NET code starts loading.

Static image loads using native code yet before .NET is initialized. Very effective to inform the user as soon as possible. There is a special way to achieve this.

Phase 2. Display custom form once .NET is loaded.

Design your custom form looking identically to your static splash screen when initially displayed. Showing the form will automatically fade-out static splash screen shown in Phase 1 (it is done by WPF) and since then you are free to display loading progress of your application. Hide this form (which can be always-on-top window) when your main window finishes loading its data.

人疚 2024-11-16 17:53:59

如果您的启动屏幕只是一个图像 - 将图像添加到项目中并将其“构建操作”属性设置为“SplashScreen”。框架将处理其余的事情。 (VS2008 sp1 或更高版本)。

如果您需要不同的启动屏幕(可能显示版本号),则此方法将不起作用。如果您想要此功能,请查看 SplashScreen 类这提供了更多的灵活性。

If your splash screen is just an image - add the image to the project and set it's Build Action property to 'SplashScreen'. The framework will handle the rest. (VS2008 sp1 or later).

If you need the splash screen to differ (maybe displaying a version number) this approach wont work. If you want this take a look at the SplashScreen class that gives a little more flexibility.

梦醒灬来后我 2024-11-16 17:53:59

可能是因为您的数据加载已完成,但您的 UI 线程尚未完成渲染。请确保在关闭启动画面之前您的 UI 已完全渲染。

看看下面的链接。

http://www.codeproject.com/KB/WPF/WPFsplashscreen.aspx

http://www.japf.fr/ 2009/10/在-wpf-应用程序中测量渲染时间/

May be because your data loading is complete but your UI thread have not finished rendering yet.Make sure that your UI is completely rendered before closing the splash.

Have a look the below links.

http://www.codeproject.com/KB/WPF/WPFsplashscreen.aspx

http://www.japf.fr/2009/10/measure-rendering-time-in-a-wpf-application/

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