Showing a Splash Screen whilst initializing a WPF Prism application

本文介绍了一种在Prism应用程序加载期间显示启动画面的方法。通过在Bootstrapper中实现,可以在模块加载完毕后自动关闭启动画面,并显示应用程序主界面。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

I hosted an internal session on Patterns & Practices Prism (or Composite Application Guidance) and one of the attendees asked if there was a way I could avoid showing the application’s shell in its unpopulated state whilst the Modules load.

There are a number of ways of doing this but I thought I’d share the approach I use in one of my applications.

Now, there’s obviously going to be some delay to your app whilst Prism loads your modules, or you wouldn’t be here, and we don’t want to risk the user trying to start multiple instances of the application or wondering what’s going on – so let’s show a Splash screen.

I chose to implement the splash screen inside my Prism bootstrapper where I create the Shell. Here’s the typical code

public override DependencyObject CreateShell()
{
    Shell shell = new Shell();
    shell.Show();
    return shell;
}

And here’s what I’d change to introduce a splash screen (e.g. SplashScreen.xaml) that closes when Prism is done loading modules:

public override DependencyObject CreateShell()
{
    SplashScreen splash = new SplashScreen();
    splash.Show();
    Shell shell = new Shell();
    shell.Dispatcher.BeginInvoke((Action) delegate
        {
            shell.Show();
            splash.Close();
        });
    return shell;
}

This takes advantage of the fact that the Bootstrapper and ModuleLoader run on the UI thread and queues a delegate on the Dispatcher that will only get invoked when all the other stuff is done.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值