WPF闪屏,直到Windows加载结束 [英] WPF splash screen until windows ends loading

查看:21
本文介绍了WPF闪屏,直到Windows加载结束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,它在加载窗口时会消耗大量时间。 在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加载函数将在启动窗口完成显示后运行。

谢谢!

推荐答案

如果您使用内置的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));
    }
}

这篇关于WPF闪屏,直到Windows加载结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆