I have a very simple demo application that I use for testing purposes. I just added a resource dictionary to it and now WPF completely fails to find any resources at runtime. Visual Studio regards those resources just fine and shows all its styles in the visual designer, but when the application is run, I get a XamlParseException
saying that the resource was not found. This code does nothing else than my other working applications, I can't find any difference. What's the problem with that?
Here's an example of the resource dictionary AppResources.xaml:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<!-- There need to be at least two styles if StartupUri is not used. -->
<!-- See http://bengribaudo.com/blog/2010/08/19/106/bug-single-application-resources-entry-ignored -->
<Style x:Key="__unused"/>
<Style x:Key="InfoLabelStyle" TargetType="TextBlock">
<Setter Property="Foreground" Value="Green"/>
</Style>
</ResourceDictionary>
Referenced from App.xaml:
<Application
x:Class="DemoApp.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Resources/AppResources.xaml"/>
<ResourceDictionary Source="/Resources/AppResources2.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
And this is the MainWindow.xaml that fails to run because of "InfoLabelStyle" not found, while the text already appears green in the designer:
<Window
x:Class="DemoApp.View.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="DemoApp"
Height="150" Width="525">
<Grid>
<TextBlock
Margin="12"
Style="{StaticResource InfoLabelStyle}"
Text="Info"/>
</Grid>
</Window>
You can find the full project source code here: http://unclassified.de/tmp/DemoApp.zip