1

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

ygoe
  • 18,655
  • 23
  • 113
  • 210

1 Answers1

1

Add a call to the Application's InitializeComponent method in your Main entry point:

[STAThread]
public static void Main()
{
    AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
    TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException;

    var app = new App();
    app.InitializeComponent();
    app.Run();
}

What does InitializeComponent() do, and how does it work in WPF?

How to write custom Main method for a WPF application?

Community
  • 1
  • 1
Szabolcs Dézsi
  • 8,743
  • 21
  • 29
  • In the infosysblogs.com article, they forgot to call `InitializeComponent` in their Approach 3 (which I do), didn't they? – ygoe Jan 16 '16 at 11:54