WPF 启动画面,直到 Windows 结束加载

标签 wpf splash-screen

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

谢谢!

最佳答案

如果您使用内置 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 结束加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6121892/

相关文章:

半透明启动画面导致 Android 崩溃

android - Flutter android版本卡在启动画面上

reactjs - 我想在初始屏幕 React Native 中播放 lottie 动画

c# - 类库项目中的 .NET 设置文件

wpf - WP8.1 中的 XAML : Child Element Styles

c# - 以编程方式如何在 MultiBinding StringFormat 中提供连续的任意数量的空白字符?

wpf - 如何替换 WPF 控件的默认样式?

c# - wpf 组合框绑定(bind)

Android:将可绘制对象添加到 styles.xml 时出现 Resources$NotFoundException

iphone - 动画/视频作为启动图像 ios