0

Heyho...

My WPF application has a customized startup to show a custom splash screen and doing some preparation (e.g. parsing arguments, prepare filesystem, ...). For this I override the OnStartup Method.

App.xaml.cs

public partial class App : Application
{
    protected override void OnStartup(StartupEventArgs e)
    {
      base.OnStartup(e);

      var splash = new SplashWindow();
      splash.Show();

      // do some black magic

      splash.Close();
      var mw = new MainWindow();
      mw.Show();
    }
  }
}

App.xaml

<Application x:Class="Example.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:WpfApp1"
             ShutdownMode="OnExplicitShutdown">
    <Application.Resources>
        <ResourceDictionary>
            <Color x:Key="Foo">#FFFFAA00</Color>
            <SolidColorBrush x:Key="Bar" Color="{StaticResource Foo}" />
        </ResourceDictionary>
    </Application.Resources>
</Application>

But handling the start up this way I cannot access resources defined in app.xaml i.e., in MainWindow.xaml or Splash.xaml.

Some other had already the same issue, i.e., https://stackoverflow.com/a/13425695

Following the workaround link takes me to this stackoverflow post: https://stackoverflow.com/a/3896209/5663348
The suggested workaround is to define x:Name of the Application element. But how does it help me accessing the defined resource? Unfortunately the link for "more information" is broken (too old)...
Applying the accepted answer of this question does even not solve the problem.

I'm using .Net Framework 7.4.2

Can someone give me some hints, how I can solve this?

Cheers and thanks :)

=================

Problem solved... The real world app had initialized MainWindow and SplashWindow while the App class was instanciated... stupid mistake...

D. Weber
  • 503
  • 5
  • 21
  • 2
    Where are you trying to access the resource and how? Overriding the `Startup` method should not affect the resources. How does your `App.xaml` look like? – mm8 May 27 '19 at 13:26
  • @mm8 like i said i try to access the resource i.e., in MainWindow and SplashWindow (added last one). I also added a example App.xaml. In both Windows I get a System.Windows.StaticResourceExntesion Exception where the keys `Foo` and `Bar` are unknown – D. Weber May 27 '19 at 14:39
  • `InitializeComponent()` throws the exception. The exception Message only referce to the row and line in the xaml file which uses `{StaticResource Foo}` or `{StaticResource Bar}` – D. Weber May 27 '19 at 14:47
  • I added how I open/close the SplashWindow and MainWindow. When I'm at home, I will try to reproduce it to provide an example project – D. Weber May 27 '19 at 14:52
  • On what line does it throw and where in the XAML are you using the resource? – mm8 May 27 '19 at 14:53
  • 1
    That worked fine for me when I tried to reproduce your issue. What exactly is the xaml for whichever window fails first? – Andy May 27 '19 at 15:36
  • Hm okay... While reconstructing the app at home...It looks like the project is screwed up somehow. but to answer your question, which line the exception throws: Its the line in the MainWindow constructor, which calls `InitializeComponent()` (In a new project its line 25). But the Exception message refers to MainWindow.xaml Line 9, where a StaticResource is used, which is defined in App.xaml. But thx for ur help. Tomorrow i will clean up the whole project to find the mistake... I will post a new comment if im not able to find it... – D. Weber May 27 '19 at 15:39
  • Okay... I found the mistake... in the app where that approach doesn't work MainWindow and SplashWindow were initlizied in the Constructor of the App class... so they cannot access static ressources defined in the App class.... But thx for ur help :) And sorry for wasting your time (or something like that) – D. Weber May 28 '19 at 08:26
  • 1
    @D.Weber My suggestion for you would be to answer your own question with how you solved it. It can potentially help others in the future. – default May 28 '19 at 09:07

1 Answers1

1

The Problem
SplashWindow and MainWindow tried to access Resources defined in App.xaml. In App.xaml.cs was some preparation done et al. loading some files. But while App was instancied it already tried to create an instance of SplashWindow and MainWindow

public partial class App : Application
{
    private readonly SplashWindow splash;

    public App() {
        splash = new SplashWindow();  // this cannot work, if SplashWindow has a reference to a resource defined in App
        this.MainWindow = new MainWindow();  // this cannot work, if MainWindow has a reference to a resource defined in App
    }

    protected override void OnStartup(StartupEventArgs e) {
        base.OnStartup(e);

        var splash = new SplashWindow(); // yes, splash was instancied multiple times...
        splash.Show();

        // do some magic

        splash.Close();
        MainWindow.Show();
    }
}

The instanciation of SplashScreen and MainWindow caused the Exception, cause they tried to access resources which are defined in App, but App wasn't finish instanciated, so the Object couldn't be accessed.
While trying to rebuild the error I created a much simpler App, where MainWindow and SplashWindow where instanciated in App.OnStartup(e), but when this Event is called the App object exists, so it can work.

The Solution
Removing the field splash and don't use the constructor to create instances of SplashWindow and MainWindow

public partial class App : Application
{
    protected override void OnStartup(StartupEventArgs e) {
        base.OnStartup(e);

        var splash = new SplashWindow(); // yes, splash was instancied multiple times...
        splash.Show();

        // do some magic

        this.MainWindow = new MainWindow();
        splash.Close();
        MainWindow.Show();
    }
}

Thx to all who helped me to find out this mistake...
And a special thanks to @Default, who rembered me to answer this question :)

D. Weber
  • 503
  • 5
  • 21