Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use .NET 4 SplashScreen in a WPF Prism based application?

Tags:

c#

.net

wpf

prism

I am trying to use a .NET 4 SplashScreen in a Prism based WPF application. I have used the SpashScreen by setting the build action on the image to SplashScreen.

The application used to keep on crashing with a System.Resources.MissingManifestResourceException. Finally I figured out that if I add a StartupUri="MainWindow.xaml" in the App.Xaml file, the SplashScreen works fine.

 <Application x:Class="Application"
       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
       StartupUri="MainWindow.xaml">
 </Application>

But in a prism application, we cannot have a StartupUri. Everything is done in the Bootstrapper.

So what do I need to do manually that StartupUri did to make the SplashScreen work?

Update 1: The complete exception message is:

System.Resources.MissingManifestResourceException was unhandled

Message=Could not find any resources appropriate for the specified culture or the neutral culture. Make sure "Application.g.resources" was correctly embedded or linked into assembly "Application" at compile time, or that all the satellite assemblies required are loadable and fully signed.

Update 2: I have figured out the adding or removing the StartupUri does not matter. What matters is that I have an additional WPF Window (other than App.xaml) or 2 dummy entries in the App.Resources tag.

<Application.Resources>
   <Style x:Key="Dummy"/>
   <Style x:Key="Dummy1"/>
</Application.Resources>

If I do not do this, the Application.g.resources file is not created in obj file and hence not embedded in the executable. Adding two dummy resource entries was brought to my attention by this blog post.

Update 3:

My question was answered by Bob Bao on MSDN forum here. Also It seems Kent was trying to point me in the same direction.

Do not set the build action of the image to SplashScreen. Instead:

Add the code in the App OnStartup method as:

protected override void OnStartup(StartupEventArgs e)
{
  SplashScreen splashScreen = new SplashScreen("splashscreen.png");
  splashScreen.Show(true);

  base.OnStartup(e);
  Bootstrapper bootstrapper = new Bootstrapper();
  bootstrapper.Run();
}

"splashscreen.png" is one image in the project, and its "Build Action" is "Resource".

like image 377
Ganesh R. Avatar asked Nov 13 '22 15:11

Ganesh R.


1 Answers

Simply define your own entry point which firstly shows the splash screen and then bootstraps Prism. In your project properties, set the entry point to your custom entry point.

internal static class Entry
{
    public static void Main(string[] args)
    {
        var splashScreen = ...;
        splashScreen.Show();

        var bootstrapper = ...;
        bootstrapper....;
    }
}
like image 111
Kent Boogaart Avatar answered Dec 22 '22 09:12

Kent Boogaart