Search code examples
.netwpfxamlresourcedictionarymergeddictionaries

Unable to find MergedDictionaries in app.xaml


I have created a WPF application (named "Ferhad.Wpf"). Then I added two class libraries with names "Ferhad.Wpf.Core" and "Ferhad.Wpf.SystemStyles" to the solution.

There is only "Resources.xaml" in "Ferhad.Wpf.Core".

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <SolidColorBrush x:Key="Foreground" Color="#FFFFFF" />
    <SolidColorBrush x:Key="BackgroundNormal" Color="#3F3F46" />
    <SolidColorBrush x:Key="BorderBrushNormal" Color="#54545C" />
    <SolidColorBrush x:Key="BorderBrushHighlighted" Color="#6A6A75" />
</ResourceDictionary>

But there are also "ButtonStyles.xaml" and "Styles.xaml" in "Ferhad.Wpf.SystemStyles". Here is "ButtonStyles.xaml":

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="pack://application:,,,/Ferhad.Wpf.Core;component/Resources.xaml" />
    </ResourceDictionary.MergedDictionaries>
    <Style x:Key="StandartButton" TargetType="Button">
        <Setter Property="Visibility" Value="Visible" />
        <Setter Property="Foreground" Value="{StaticResource Foreground}" />
        <Setter Property="Background" Value="{StaticResource BackgroundNormal}" />
        <Setter Property="BorderBrush" Value="{StaticResource BorderBrushNormal}" />
    </Style>
</ResourceDictionary>

and "Styles.xaml":

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="ButtonStyles.xaml" />
    </ResourceDictionary.MergedDictionaries>

    <Style TargetType="Button" BasedOn="{StaticResource StandartButton}" />
</ResourceDictionary>

And here is app.xaml:

<Application x:Class="Ferhad.Wpf.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="pack://application:,,,/Ferhad.Wpf.SystemStyles;component/Styles.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

Everything seems fine at design time of MainWindow.xaml, but when running the application an exception is thrown: Set property 'System.Windows.ResourceDictionary.Source' threw an exception.

I have googled about it, but nothing has helped. I have tried to change the BuildAction of files, but that did not help either.

UPDATE:

The exception is on line number '8' and line position '18'.

And the inner exception is: {"Could not load file or assembly 'Ferhad.Wpf.SystemStyles, Culture=neutral' or one of its dependencies. The system cannot find the file specified.":"Ferhad.Wpf.SystemStyles, Culture=neutral"}


Solution

  • You probably forgot to add a reference from App to Core, SystemStyles needs the Core so if the build process only copies the SystemStyles it does not have its Core assembly.

    App
    -> SystemStyles
    -> Core
    
    SystemStyles
    -> Core //Not strictly necessary because the build process does not copy the 
            //reference to the App project anyway and the resources
            //are not resolved at compile-time
    
    Core
    -