Search code examples
c#wpfxamlresourcedictionary

Why are Application-Resources only loaded when placed in a seperate file if OnStartup is overriden?


In App.xaml (no StartupUri):

...
<Application.Resources>
    <ResourceDictionary Source="AppResources.xaml"/>
</Application.Resources>
...

This does work but declaring the ResourceDictionary directly in Application.Resources does not:

...
<Application.Resources >
    <ResourceDictionary>
        ...
    </ResourceDictionary>
</Application.Resources>
...

OnStartup in App.xaml.cs:

protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);
        if (e.Args.Length > 0)
        {
            // Handle args and start headless.
        }
        else
        {
            // Create window.
            new Views.ShellWindow().ShowDialog();
        }
        this.Shutdown();
    }

How can this behaviour be fixed?

Edit: I am using Visual Studio 2013 Professional.


Solution

  • Apparently, there is a VS code generation bug where the code necessary to connect to the rest of the program sometimes is not inserted when

    • The application has no StartupUri attribute set in App.xaml AND
    • there is only one resoure in the section of App.xaml

    A possible workaround is to use an event instead of overriding OnStartup, see WPF - App.xaml file does not get parsed if my app does not set a StartupUri? for more information.