Bug: Single <Application.Resources> Entry Ignored

, , , ,

Crash! The offending XAML sets a control’s style property to a StaticResource (e.g. <Button Style=”{StaticResource myStyle}” />). The named style is defined in <Application.Resources>. Why then the XamlParseException “Cannot find resource…” exception?

In the program’s App.xaml file:

  • The <Application> tag does not have a StartupUri attribute. (Instead, the program’s initial window is launched by an override of Application’s OnStartup method in App.xaml.cs.)
  • <Application.Resources> contains only one entry.

Still don’t see the problem? Well, there shouldn’t be one. The above-described XAML is valid. Alas! A Microsoft Visual Studio bug that breaks <Application.Resources> sometimes appears when these two factors are present.

A simple work-around: Make sure that <Application.Resources> contains at least two entries. If your application only uses one entry in <Application.Resources>, insert a second, dummy entry (like <Style x:Key=”unused” />) to bypass this bug.

Technical Details

Visual Studio auto-generates a hidden file named App.g.cs which “wires” App.xaml into the application. When the above-mentioned two factors are present, the code generator sometimes fails to insert the code that loads <Application.Resources>’s contents into the application. If this loading does not occur, other XAML files in the program will be unable to use the resources defined in <Application.Resources>.

This issue has been reported to Microsoft.

9 thoughts on “Bug: Single <Application.Resources> Entry Ignored

  1. Mo

    Thanks for blogging this one Ben! I ran into the same issue a few weeks ago and decided to postpone and focus my efforts on something else after much head scratching. Today I tried again to no avail and google was kind enough to point me to your blog 🙂

    I’m using the dummy approach for now, will probably have to add more entries anyway as I go along. I just could’t figure out why the heck it wasn’t working with my single entry. Never bothered trying to add more or hooking up StartupUri again etc., just assumed it must have been me doing it wrong… lol

    Reply
  2. Pingback: How to use .NET 4 SplashScreen in a WPF Prism based application?

  3. Rich

    Ben Gribaudo (or anyone else who can help),

    I’ve tried setting the StartupUri property in the App.xaml file instead of calling [mainwindow].ShowDialog() in the OnStartup overload. This did not resolve the issue, however. I then tried also setting the Startup attribute in the App.xaml file and completely replaced the OnStartup overload with the Startup event handler. This still not resolve the issue.

    I can’t try the dummy resource approach apparently because my [Application.Resources] tag contains just a [ResourceDictionary] which merges all of my various resource files. If I try to add another dummy resource inside the [Application.Resources] tag than the XAML parser complains about the resource dictionary not having a key. If I add a key to the resource dictionary than that causes all sorts of other issues.

    Any ideas? I should also mention that I’m only getting these “Cannot find resource…” exceptions when I load the XAML file in the visual studio designer. At run-time everything works fine. Also, if close the file after the designer throws the exception and then re-open it all xaml files start working correctly.

    Thoroughly confused.

    Reply
  4. Mike Hanson

    Thaaaaaaannnnnnk Youu!

    To Microsoft: This blog was posted back in August 2010. People are still having this issue. It took me two hours of searching the internet before I found this post. Get with the program, NOW! I paid $499.00 for the professional version of VS 2017 and this bug still exists.

    Reply

Leave a Reply to Bart Donders Cancel reply

Your email address will not be published. Required fields are marked *